我有一个具有收集状态的mongo服务器。无论何时添加新状态,都会保存在我的服务器中。但是当我尝试在前端编辑集合时,它会被保存为我的服务器中具有不同id的另一个新集合。如何克服这一点。
public static void CreateData(string pass ,string pass1)
{
string StateCode = pass;
string stateName = pass1;
insertState(StateCode, stateName);
}
private static void insertState(string StateCode, string stateName)
{
MongoServer server = MongoServer.Create(ConnectionString);
MongoDatabase myCompany = server.GetDatabase("BackOffice");
MongoCollection<BsonDocument> tblStates = myCompany.GetCollection<BsonDocument>("tblStates");
BsonDocument deptartment = new BsonDocument {
{ "State_strCode", StateCode },
{ "State_strName", stateName },
{"States_strIsActive","Y"},
{"State_strFedExStateCode",""},
{"State_dtmTimeStamp",DateTime.Now}
};
tblStates.Insert(deptartment);
}
答案 0 :(得分:1)
您最好定义一个结构或设置PK:
public class Customer
{
public string name { get; set; }
[MongoDB.Bson.Serialization.Attributes.BsonId]
public string idCol { get; set; }
}
并像这样更新您的对象
public void Update(Customer cus)
{
if (cus != null)
{
MongoDB.Driver.MongoClient client = new MongoClient(_connectionString);
MongoDatabase db = client.GetServer().GetDatabase(_dbName);
db.GetCollection(_dbName).Save(cus);
}
}