0

i have one collection in which i am doing insert/update operation. for insert i use the code:

MongoCollection<BsonDocument> tblCity = mydb.GetCollection<BsonDocument>("tblCity");
BsonDocument CollectionCity = new BsonDocument {
                    { "CityCode", cityCode },
                    { "CityName", cityName },
                    { "stamps" , new BsonDocument { 
                        {"ins", DateTime.Now}, 
                        {"upd", ""}, 
                        {"createUsr", UserId}, 
                        {"updUsr", ""}, 
                        {"Ins_Ip", ""},
                        {"Upd_IP", GetIP()}
                        }
                    }
                };
        tblCity.Insert(CollectionCity);

it is working fine. but while i am updating i am using code:

MongoCollection <BsonDocument>  tblCity = mydb.GetCollection<BsonDocument>("tblCity"); 
var query = new QueryDocument { { "City_strCode", cityCode } };
var update = new UpdateDocument {
     { "$set", new BsonDocument("City_strName", cityName) },
     { "stamps" , new BsonDocument{
         {"upd",  DateTime.Now}, 
         {"updUsr", ""}, 
         {"Upd_IP", GetIP()
     }}
}};

tblCity.Update(query, update);

But problem is that with out changing the ins date i want to update upd field. But it is removing the ins field and updating the upd field. I am trying a lot of ways but not able to get any solution. Please suggest something....Even I got some links based on this and tried.. but none of them workout.

4

1 回答 1

0

You'll need to fix your use of $set and the query.

Your query doesn't match with the field name in the inserted document.

var query = new QueryDocument { { "CityCode", cityCode  } };

If you're using $set, then pass all of the fields you want to change as part of the BsonDocument:

var query = new QueryDocument { { "CityCode", cityCode  } };
var update = new UpdateDocument {
        { "$set", new BsonDocument {
        { "CityName", "San Fran"}, 
        { "stamps.upd" , DateTime.Now()}, 
        { "stamps.updUsr", ""}, 
        { "stamps.Upd_IP", "10.0.0.1" }
}}};
于 2013-05-29T12:51:19.810 回答