Populating MongoDB Collections: Efficiently Inserting Documents

Learn how to add documents to MongoDB collections using the insertOne(), insert(), and insertMany() methods. This guide will provide you with the necessary knowledge to effectively insert single or multiple documents into your MongoDB databases, streamlining your data management processes.



MongoDB - Insert Multiple Documents in a Collection

In MongoDB, you can insert one or more documents into a collection using the following methods:

  • insertOne() - Inserts a single document.
  • insert() - Inserts one or more documents.
  • insertMany() - Inserts multiple documents.

Using insert()

The insert() method can insert either a single document or an array of documents into a collection. The syntax is:

Syntax

db.collection.insert(
    document or array of documents, 
    [writeConcern], 
    [ordered]
)
        

Parameters:

  • document or array of documents: A single document or an array of documents to insert.
  • writeConcern: Optional. Document expressing the write concern.
  • ordered: Optional. Boolean indicating if the insert should be ordered (true) or unordered (false). Defaults to true.

Example: Insert a Single Document

Example: Insert Single Document

db.employees.insert({ 
    firstName: "John",
    lastName: "King",
    email: "john.king@abc.com"
})
        

Output:

Output

{
  acknowledged: true,
  insertedIds: { '0': ObjectId("616d62d9a861820797edd9b2") }
}
        

Example: Insert Multiple Documents

Example: Insert Multiple Documents

db.employees.insert([
    { 
        firstName: "John",
        lastName: "King",
        email: "john.king@abc.com"
    },
    { 
        firstName: "Sachin",
        lastName: "T",
        email: "sachin.t@abc.com"
    },
    { 
        firstName: "James",
        lastName: "Bond",
        email: "jamesb@abc.com"
    }
])
        

Output:

Output

{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId("616d63eda861820797edd9b3"),
    '1': ObjectId("616d63eda861820797edd9b4"),
    '2': ObjectId("616d63eda861820797edd9b5")
  }
}
        

Documents can also be inserted with custom _id values:

Example: Insert Documents with Custom _id

db.employees.insert([
    { 
        _id: 1,
        firstName: "John",
        lastName: "King",
        email: "john.king@abc.com"
    },
    { 
        _id: 2,
        firstName: "Sachin",
        lastName: "T",
        email: "sachin.t@abc.com"
    },
    { 
        _id: 3,
        firstName: "James",
        lastName: "Bond",
        email: "jamesb@abc.com"
    }
])
        

Output:

Output

{
  acknowledged: true,
  insertedIds: { '0': 1, '1': 2, '2': 3 }
}
        

By default, insert() performs ordered inserts. If an error occurs, remaining documents will not be processed. You can set ordered: false to insert all documents even if errors occur:

Example: Unordered Insert

db.employees.insert(
    [
        { 
            firstName: "Steve",
            lastName: "J",
            email: "steve.j@abc.com"
        },
        { 
            _id: 1,
            firstName: "Kapil",
            lastName: "D",
            email: "kapil.d@abc.com"
        },
        { 
            firstName: "Amitabh",
            lastName: "B",
            email: "amitabh.b@abc.com"
        }
    ],
    { ordered: false }
)
        

Output:

Output

{
  acknowledged: true,
  insertedIds: [
    { index: 0, _id: ObjectId("616e6be33fa8bd4420d49373") },
    { index: 1, _id: 1 },
    { index: 2, _id: ObjectId("616e6be33fa8bd4420d49374") }
  ]
}
        

Using insertMany()

The insertMany() method inserts multiple documents and cannot be used to insert a single document. The syntax is:

Syntax

db.collection.insertMany(
   [document1, document2, ....],
   {
     writeConcern: ,
     ordered: 
   }
)
        

Example: Insert Multiple Documents

Example: Insert Multiple Documents

db.employees.insertMany([
    { 
        firstName: "John",
        lastName: "King",
        email: "john.king@abc.com"
    },
    { 
        firstName: "Sachin",
        lastName: "T",
        email: "sachin.t@abc.com"
    },
    { 
        firstName: "James",
        lastName: "Bond",
        email: "jamesb@abc.com"
    }
])
        

Output:

Output

{
  acknowledged: true,
  insertedIds: { '0': ObjectId("616d63eda861820797edd9b3"), '1': ObjectId("616d63eda861820797edd9b4"), '2': ObjectId("616d63eda861820797edd9b5") }
}
        

Example with custom _id values:

Example: insertMany() with Custom _id

db.employees.insertMany([
    { 
        _id: 1,
        firstName: "John",
        lastName: "King",
        email: "john.king@abc.com",
        salary: 5000
    },
    { 
        _id: 2,
        firstName: "Sachin",
        lastName: "T",
        email: "sachin.t@abc.com",
        salary: 8000
    },
    { 
        _id: 3,
        firstName: "James",
        lastName: "Bond",
        email: "jamesb@abc.com",
        salary: 7500
    },
    { 
        _id: 4,
        firstName: "Steve",
        lastName: "J",
        email: "steve.j@abc.com",
        salary: 9000
    },
    { 
        _id: 5,
        firstName: "Kapil",
        lastName: "D",
        email: "kapil.d@abc.com",
        salary: 4500
    },
    { 
        _id: 6,
        firstName: "Amitabh",
        lastName: "B",
        email: "amitabh.b@abc.com",
        salary: 11000
    }
])
        

Output:

Output

{
  acknowledged: true,
  insertedIds: { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5, '5': 6 }
}
        

Both methods will create the collection if it does not exist. MongoDB is case-sensitive, so "employees" and "Employees" are treated as different collections.