Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to you make changes of document properties in MongoDB

I`m new to MongoDB and i just successfully migrated from MS SQL to MongoDB. When i made my custom classes for MongoDB collections i had some extra property whitch i dont need it.

How i can remove it?

For example lets say i have basic class

public class Basic {

public ObjectId _id {get;set;}
public string Name {get;set;
public string Description {get;set;

}

Then i store some item in Mongo

var collection = Mongo.GetCollection<Basic>("basic");
var item = new Basic{ Name = "TestName", Description = "Remove me"};
collection.insert(item);

When i make changes for example i dont need Description property in my Basic class, i want to remove every Description property in all documents.

like image 469
Novkovski Stevo Bato Avatar asked Jan 29 '26 11:01

Novkovski Stevo Bato


1 Answers

You can execute a mass update. This is how you do it in mongo shell:

db.yourcollection.update({}, {$unset: {description: 1}}, false, true);

Documentation on $unset: http://www.mongodb.org/display/DOCS/Updating#Updating-%24unset Documentation on update(): http://www.mongodb.org/display/DOCS/Updating#Updating-update%28%29

like image 182
Sergio Tulentsev Avatar answered Jan 31 '26 04:01

Sergio Tulentsev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!