MongoDB: Insert Multiple Documents in easy way

0 0
Read Time:1 Minute, 56 Second

MongoDB: Insert Multiple Documents

1. Insert Multiple Document Using: insert() function (deprecated)

The db.<collection>.insert() method inserts one document or an array of documents into a collection

The following inserts a single document. It’s the same as the insertOne() method.

Example: insert() Copy

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

Output

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

The following inserts multiple documents into a collection by passing an array of documents.

Example: Insert a Document

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

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

You can specify a different _id field value into one or more documents, as shown below.

Example: Insert a Document

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

Output

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

2. Insert Multiple Document Using: insertMany() function

The db.<collection>.insertMany() inserts multiple documents into a collection. It cannot insert a single document.

The following adds multiple documents using the insertMany() method.

Example: Insert a Document Copy

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

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

The following inserts 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

{
  acknowledged: true,
  insertedIds: { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5, '5': 6 }
}
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Comment