Computer >> Computer tutorials >  >> Programming >> MongoDB

How to use collMod in MongoDB runCommand()?


The collMod makes it possible to add options to a collection or to modify view definitions. You can use runCommand() along with collMod(). Let us first create a collection with documents −

> db.demo13.insertOne({"StudentFirstName":"Chris"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e0f730ad7df943a7cec4fa6")
}
> db.demo13.insertOne({"StudentFirstName":"David"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e0f7310d7df943a7cec4fa7")
}
> db.demo13.insertOne({"StudentFirstName":"Bob"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e0f7313d7df943a7cec4fa8")
}

Following is the query to display all documents from a collection with the help of find() method −

> db.demo13.find();

This will produce the following output −

{ "_id" : ObjectId("5e0f730ad7df943a7cec4fa6"), "StudentFirstName" : "Chris" }
{ "_id" : ObjectId("5e0f7310d7df943a7cec4fa7"), "StudentFirstName" : "David" }
{ "_id" : ObjectId("5e0f7313d7df943a7cec4fa8"), "StudentFirstName" : "Bob" }

Following is the query to use collMod −

> db.runCommand( { collMod: "demo13", usePowerOf2Sizes: false })

This will produce the following output −

{ "usePowerOf2Sizes_old" : true, "usePowerOf2Sizes_new" : false, "ok" : 1 }