我需要在我的MongoDB集合創建一個時間戳。正在使用C#在前端。我的代碼是:如何使插入和更新蒙哥記錄時間戳差?
internal static void CreateStudent(string Id, string Name,string strUserId)
{
MongoServer server = MongoServer.Create(ConnectionString);
MongoDatabase mydb = server.GetDatabase("Database");
MongoCollection<BsonDocument> Student = mydb.GetCollection<BsonDocument>("Student");
BsonDocument colectionGenre = new BsonDocument {
{ "Code", Id }, //Id is Auto Generated in sql. Fetch from there using Output parameter and save it in one variable and pass that here
{ "Name", Name },
{ "Status","Y"},
{"stamps" , new BsonDocument {
{"Ins_date", DateTime.Now},
{"up_date",""},
{"createUsr", strUserId},
{"updUsr", ""},
{"Ins_Ip", GetIP()},
{"Upd_IP",""}}}
};
Student.Insert(colectionGenre);
}
internal static void UpdateStudent(string Id, string Name,string strUserId)
{
MongoServer server = MongoServer.Create(ConnectionString);
MongoDatabase mydb = server.GetDatabase("Database");
MongoCollection<BsonDocument>Student = mydb.GetCollection<BsonDocument>("Student"); ;
// Query for fetch the ID which is edited by the User...(user can only able to edit the NAME field alone)
var query = new QueryDocument {
{ "Code", Id }};
// After Fetch the correspondent ID it updates the name with the Edited one
var update = new UpdateDocument {
{ "$set", new BsonDocument("Name", Name) }
};
// Updated Query.(Id is same as previous. Name is updated with new one)
{"stamps" , new BsonDocument {
{"up_date",DateTime.Now},
{"updUsr", strUserId},
{"Upd_IP",GetIp()}}}
}}
};
Student.Update(query,update,UpdateFlags.Upsert, SafeMode.True);
}
它工作正常的INSERT方法與時間(蓋章)一旦創建了記錄。但問題是更新方法。當用戶更新的東西插入時間也發生了變化與更新的時間..
後用戶更新的名字,我想我會收集看起來像這樣
{
"_id" : ObjectId("5178aea4e6d8e401e8e51dc0"),
"Code": 12,
"Name": Sname,
"Stamps:"{
"Ins_date":03:34:00,
"up_date": 04:35:12
}
}
但我的問題是,無論是時間相同之後更新。這是因爲它需要當前的日期和時間函數..我可以如何實現上述輸出。它需要任何驅動程序。建議給我一些東西...
當你說你不希望它被改變時,爲什麼要設置插入日期值?更新與現有文檔合併,並且不會替換。 – WiredPrairie 2013-05-08 10:48:36
你使用了upsert標誌嗎?我沒有在代碼中看到它,但它聽起來像你不知道記錄是否會被更新或插入? – 2013-05-08 17:35:09
@AsyaKamsky我試圖用這樣的(翻轉標誌UpdateFlags.Upsert,SafeMode.True)..但它仍然是相同的。 – Aarthi 2013-05-25 09:38:16