Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB: update only specific fields

I am trying to update a row in a (typed) MongoDB collection with the C# driver. When handling data of that particular collection of type MongoCollection<User>, I tend to avoid retrieving sensitive data from the collection (salt, password hash, etc.)

Now I am trying to update a User instance. However, I never actually retrieved sensitive data in the first place, so I guess this data would be default(byte[]) in the retrieved model instance (as far as I can tell) before I apply modifications and submit the new data to the collection.

Maybe I am overseeing something trivial in the MongoDB C# driver how I can use MongoCollection<T>.Save(T item) without updating specific properties such as User.PasswordHash or User.PasswordSalt? Should I retrieve the full record first, update "safe" properties there, and write it back? Or is there a fancy option to exclude certain fields from the update?

Thanks in advance

like image 600
Manny Avatar asked Feb 13 '13 12:02

Manny


People also ask

How do I update a specific field in MongoDB?

We can use $set and $inc operators to update any field in MongoDB. The $set operator will set the newly specified value while the $inc operator will increase the value by a specified value.

Which of the following is needed to update only specific field using MongoDB?

Replace field values of a document using the $set operator If only one field of a document should be updated the "$set" operator can be used. The set operator will override the old value of a specified field.

Which operator is used to update specific fields in MongoDB?

Use Update Operator Expressions ( $inc and $set ) The db. collection. update() method updates only the corresponding fields in the document.

Does MongoDB have field level updates?

One of the advantages of MongoDB over various NoSQL systems and key/value stores is the ability to update individual fields atomically in the same way developers are already used to doing in RDBMS. This is not limited to specific types of operations, and can be used with any value.


2 Answers

It´s possible to add more criterias in the Where-statement. Like this:

var db = ReferenceTreeDb.Database;
var packageCol = db.GetCollection<Package>("dotnetpackage");
var filter = Builders<Package>.Filter.Where(_ => _.packageName == packageItem.PackageName.ToLower() && _.isLatestVersion);
var update = Builders<Package>.Update.Set(_ => _.isLatestVersion, false);
var options = new FindOneAndUpdateOptions<Package>();
packageCol.FindOneAndUpdate(filter, update, options);
like image 125
user6356311 Avatar answered Oct 07 '22 06:10

user6356311


Save(someValue) is for the case where you want the resulting record to be or become the full object (someValue) you passed in.

You can use

var query = Query.EQ("_id","123");
var sortBy = SortBy.Null;
var update = Update.Inc("LoginCount",1).Set("LastLogin",DateTime.UtcNow); // some update, you can chain a series of update commands here

MongoCollection<User>.FindAndModify(query,sortby,update); 

method.

Using FindAndModify you can specify exactly which fields in an existing record to change and leave the rest alone.

You can see an example here.

The only thing you need from the existing record would be its _id, the 2 secret fields need not be loaded or ever mapped back into your POCO object.

like image 44
Nuk Nuk San Avatar answered Oct 07 '22 06:10

Nuk Nuk San