2013-05-08 119 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