2013-05-08 124 views
0

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

但我的问题是,无论是时间相同之后更新。这是因为它需要当前的日期和时间函数..我可以如何实现上述输出。它需要任何驱动程序。建议给我一些东西...

+1

当你说你不希望它被改变时,为什么要设置插入日期值?更新与现有文档合并,并且不会替换。 – WiredPrairie 2013-05-08 10:48:36

+0

你使用了upsert标志吗?我没有在代码中看到它,但它听起来像你不知道记录是否会被更新或插入? – 2013-05-08 17:35:09

+0

@AsyaKamsky我试图用这样的(翻转标志UpdateFlags.Upsert,SafeMode.True)..但它仍然是相同的。 – Aarthi 2013-05-25 09:38:16

回答

0

最后我得到了答案......在INSERT方法只是通过下面的事情

{"Insert-stamps" , new BsonDocument { 
     {"Ins_date", DateTime.Now}, 
     {"createUsr", strUserId}, 
     {"Ins_Ip", GetIP()}}}, 
    {"Update-stamps" , new BsonDocument { 
     {"up_date",""}, 
     {"updUsr", ""}, 
     {"Upd_IP",""}}} 

并在更新的方法

{"Update-stamps" , new BsonDocument { 
     {"up_date", DateTime.Now}, 
     {"updUsr", StrUserId}, 
     {"Upd_IP",GetIP()}}} 

它工作正常的我的标准....

+0

谢谢所有帮助我解决此问题的人... – Aarthi 2013-05-27 06:57:21

0

当你传递一个值为Ins_date字段正在更新文档。只需从更新文档中删除它,它不会改变它。

var update = new UpdateDocument { 
    {"$set", new BsonDocument { 
     {"State_strName", name}, 
     {"stamps" , new BsonDocument { 
      {"up_date",DateTime.Now}, 
      {"createUsr", ""}, 
      {"updUsr", ""}, 
      {"Ins_Ip", GetIP()}, 
      {"Upd_IP",GetIP()}}}    
     }; 
    tblmytbl.Update(query, update); 
+0

当我删除插入字段意味着插入时间我无法到达那里..只有更新时间将bwe在那里..我需要两个时间 – Aarthi 2013-05-08 10:29:37

+0

@Aarthi不删除插入时间字段。只需更新您的更新时间,而更新。 – sachin 2013-05-08 11:09:07

+0

我根据条件传递此代码。当用户id =“401”时,它检查条件,如果该值不存在则意味着它插入语句。所以我第一次做up_date,“”。如果值401存在它去更新部分.. – Aarthi 2013-05-08 11:21:12

0

您如何通过使用唯一的ID或其他唯一value.Check唯一ID还是值是否已更新现有文档中的价值存在于数据库中documents.If它存在的方式改变更新时间只是不会做任何事情..

+0

this答案与@matthewtole答案相同,但我认为这会帮助你理解以前的答案。 – sachin 2013-05-08 11:04:31

+0

我根据条件传递此代码。当用户id =“401”时,它检查条件,如果该值不存在则意味着它插入语句。所以我第一次做up_date,“”。如果存在值401,则更新部分。所以当updat部分执行意味着我无法获得inserrt时间在这里.. – Aarthi 2013-05-08 11:23:01

+0

@Aarthi当你更新的部分将执行,如果你已经插入文件。所以没问题。 – sachin 2013-05-08 11:25:44

0

在更新mongoDB中的数据时,您传递的是Ins_date和up_date的相同值,即DateTime.Now(当前系统日期和时间)。所以在monoDB中更新了相同的值文件。

为此,你可以做一两件事: - 更新MongoDB的文档之前,你需要从数据库Ins_date值在C#.NET中使用SQL查询,然后使用这个值Ins_date和up_date使用DateTime.Now那么你两个值都会不同。

var update = new UpdateDocument { 
     {"$set", new BsonDocument { 
      {"State_strName", name}, 
      {"stamps" , new BsonDocument { 
       {"Ins_date", **Ins_date values_from your database**} , 
       {"up_date",DateTime.Now}, 
       {"createUsr", ""}, 
       {"updUsr", ""}, 
       {"Ins_Ip", GetIP()}, 
       {"Upd_IP",GetIP()}}}    
      }; 
     tblmytbl.Update(query, update); 
0

听起来像是你需要的是新的$setOnInsert运营商这是在2.4加入正是这种使用情况。

当与插入UPSERT标志结果的更新,要$ insert_date设置为Date.now但是当它是定期更新,你不想来设置它。因此,与您的更新,现在你应该使用$set您要设置是否它是一个更新或插入普通领域,而是使用$setOnInsert对于只插入过程中设置的字段。

+0

@ Asya Kamshy我无法在代码中设置提及标志..它会抛出错误 – Aarthi 2013-05-27 07:45:56

+0

你能更具体一点吗?什么标志?什么错误? $ setOnInsert不是一个标志,它是一个操作符,像$ set – 2013-05-27 18:30:01