Let us see an example and create a collection with documents −
> db.demo557.insertOne({Name:"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f28e954b4472ed3e8e864") } > db.demo557.insertOne({Name:"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f28ee54b4472ed3e8e865") }
Display all documents from a collection with the help of find() method −
> db.demo557.find();
This will produce the following output −
{ "_id" : ObjectId("5e8f28e954b4472ed3e8e864"), "Name" : "Chris" } { "_id" : ObjectId("5e8f28ee54b4472ed3e8e865"), "Name" : "David" }
Following is the query to update only a specific field value −
> db.getCollection('demo557').update({Name:"Chris"},{$set:{Name:"Robert"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo557.find();
This will produce the following output −
{ "_id" : ObjectId("5e8f28e954b4472ed3e8e864"), "Name" : "Robert" } { "_id" : ObjectId("5e8f28ee54b4472ed3e8e865"), "Name" : "David" }