Use the db.collection.save() to update an existing document or inserts a new document, depending on its document parameter. Let us create a collection with documents −
> db.demo481.save({"FirstName":"Chris","LastName":"Brown"}); WriteResult({ "nInserted" : 1 }) > db.demo481.save({"FirstName":"David","LastName":"Miller"}); WriteResult({ "nInserted" : 1 }) > db.demo481.save({"FirstName":"John","LastName":"Doe"}); WriteResult({ "nInserted" : 1 }) > db.demo481.save({"FirstName":"John","LastName":"Smith"}); WriteResult({ "nInserted" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo481.find();
This will produce the following output −
{ "_id" : ObjectId("5e82db39b0f3fa88e227909c"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5e82db45b0f3fa88e227909d"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ObjectId("5e82db4db0f3fa88e227909e"), "FirstName" : "John", "LastName" : "Doe" } { "_id" : ObjectId("5e82db52b0f3fa88e227909f"), "FirstName" : "John", "LastName" : "Smith" }