Search code examples
mongodbmodelmongodb-.net-drivernosql

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.


Solution

  • 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