我需要在我的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