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
}
}

しかし、私の問題は、更新後に両方の時間が同じになることです。これは、現在の日付と時刻の関数を使用するためです..上記の出力を実現するにはどうすればよいですか.ドライバーが必要です.私に何かを提案してください...

4

5 に答える 5

0

一意の ID または他の一意の値を使用して、既存のドキュメントの値を更新する方法。一意の ID または値がデータベース ドキュメントに既に存在するかどうかを確認します。存在する場合は、更新時刻を変更するだけで何もしません。 .

于 2013-05-08T11:00:48.990 に答える
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);
于 2013-05-08T10:24:10.440 に答える