How to Insert Multiple Documents using insertMany()


HOW TO INSERT MULTIPLE RECORDS


This insertMany() is a MongoDB method that allows you to insert multiple documents into a collection in one operation. Here's an example of how to use insertMany() to insert multiple documents into the students collection:

db.students.insertMany([
  {"_id":3,"name":"Ramesh"},
  {"_id":4,"name":"Tiya","age":15},
  {"name":"Diya","age":5}
]);
 

In this example, we are inserting three documents into the students collection:

  • The first document has an explicitly defined _id field with a value of 3 and a name field with a value of "Ramesh".
  • The second document also has an explicitly defined _id field with a value of 4, a name field with a value of "Tiya", and an age field with a value of 15.
  • The third document has no _id field defined, so MongoDB will automatically generate an _id field for it, and it has a name field with a value of "Diya" and an age field with a value of 5.

Note that if any of the documents in the array already have an _id field that matches an existing document in the collection, MongoDB will throw a duplicate key error and the entire insertMany() operation will fail.


HOW TO SELECT RECORDS


db.students.find({},{"_id":0,"name":1,"age":1});

The MongoDB query db.students.find({},{"_id":0,"name":1,"age":1}) finds all documents in the students collection and returns only the name and age fields for each document, while excluding the _id field. Here's how this query works:

  • db.students: specifies the name of the collection to search in (in this case, students).
  • .find(): specifies that we want to find documents in the collection that match a certain criteria.
  • {},: provides an empty filter expression, which matches all documents in the collection.
  • {"_id":0,"name":1,"age":1}: specifies which fields we want to include or exclude from the result set. In this case, we're excluding the _id field (since "0" means exclude) and including the name and age fields (since "1" means include).

So, when you run this query in the MongoDB shell, it will return a cursor that contains all documents in the students collection, but with only the name and age fields included, and the _id field excluded.


Source Code

use("school");
 
//show dbs;
//use school;
 
 
db.getMongo().getDBs();
db.getCollectionNames();
 
db.students.insertOne({"name":"joes","age":35,"city":"Salem"});
db.students.find();
 
db.students.insertOne({"name":"Ram","age":25,"city":"Chennai","contact":"9876543210"});
db.students.find();
 
db.students.insertOne({"name":"Sam","age":20,"city":"Chennai","contact":"9885967895","address":{"street":"cherry Road","pincode":"636007"}});
db.students.find();
db.students.find({ name: "joes" });
db.students.insertOne({"_id": 2,"name": "Sara","hobbies": ["Dancing","Drawing", "Singing" ]});
 
 
db.students.insertMany([
  {"_id":3,"name":"Ramesh"},
  {"_id":4,"name":"Tiya","age":15},
  {"name":"Diya","age":5}
]);
 
 
db.students.find({},{"_id":0,"name":1,"age":1});

This is a sequence of MongoDB shell commands that perform various operations on the students collection within the school database. Here's an explanation of each command:

  • use("school"): selects the school database as the current database.
  • db.getMongo().getDBs() and db.getCollectionNames(): These two commands retrieve the list of all databases and collections, respectively, that are accessible by the current connection.
  • db.students.insertOne({"name":"joes","age":35,"city":"Salem"}): Inserts a new document into the students collection with the specified fields.
  • db.students.find(): Returns all documents in the students collection.
  • db.students.insertOne({"name":"Ram","age":25,"city":"Chennai","contact":"9876543210"}): Inserts a new document with an additional field, contact, into the students collection.
  • db.students.insertOne({"name":"Sam","age":20,"city":"Chennai","contact":"9885967895","address":{"street":"cherry Road","pincode":"636007"}}): Inserts a new document with an additional nested field, address, into the students collection.
  • db.students.find({ name: "joes" }): Returns all documents in the students collection that have a name field equal to "joes".
  • db.students.insertOne({"_id": 2,"name": "Sara","hobbies": ["Dancing","Drawing", "Singing" ]}): Inserts a new document into the students collection with a manually specified _id field and an additional field, hobbies, which is an array.
  • db.students.insertMany([...]): Inserts multiple documents into the students collection using the insertMany() method.
  • db.students.find({},{"_id":0,"name":1,"age":1}): Returns all documents in the students collection, but only with the name and age fields included, and the _id field excluded.