How to push value with for loop in MongoDB?

MongoDBDatabaseBig Data Analytics

To push value, use save() along with for loop. Let us create a collection with documents −

> for(var v=1; v<7; v++) {
... db.demo739.save({Name:"Chris",SubjectName:"MongoDB"});
... }
WriteResult({ "nInserted" : 1 })

Display all documents from a collection with the help of find() method −

> db.demo739.find();

This will produce the following output −

{ "_id" : ObjectId("5ead6e7857bb72a10bcf0666"), "Name" : "Chris", "SubjectName" : "MongoDB" }
{ "_id" : ObjectId("5ead6e7857bb72a10bcf0667"), "Name" : "Chris", "SubjectName" : "MongoDB" }
{ "_id" : ObjectId("5ead6e7857bb72a10bcf0668"), "Name" : "Chris", "SubjectName" : "MongoDB" }
{ "_id" : ObjectId("5ead6e7857bb72a10bcf0669"), "Name" : "Chris", "SubjectName" : "MongoDB" }
{ "_id" : ObjectId("5ead6e7857bb72a10bcf066a"), "Name" : "Chris", "SubjectName" : "MongoDB" }
{ "_id" : ObjectId("5ead6e7857bb72a10bcf066b"), "Name" : "Chris", "SubjectName" : "MongoDB" }
raja
Published on 30-Jun-2020 11:22:20
Advertisements