Syntax error How to push value with for loop in MongoDB?

How to push value with for loop in MongoDB?



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" }
Updated on: 2020-06-30T07:52:34+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements