Insert Documents in MongoDB: insertOne, insertMany, Arrays & Objects
Learn how to insert one document, many documents, arrays, and embedded objects in MongoDB, plus how to enforce uniqueness with createIndex().
Insert Documents in MongoDB
Use db.collection.insert() (or its two modern variants below) to add new documents to a MongoDB collection. You don't need to create the collection first — inserting into a non-existent collection creates it automatically.
There are two main methods for inserting documents:
insertOne()— insert a single documentinsertMany()— insert multiple documents at once
insertOne()
db.emp.insertOne({ "name": "Rahul", "email": "rahul@gmail.com" })
// {
// acknowledged: true,
// insertedId: ObjectId("6490ae32a1ada8a3a0c0ad76")
// }
Nothing stops you from inserting a second document with the same email:
db.emp.insertOne({ "name": "Rahil", "email": "rahul@gmail.com" })
Two employees now share the same email — MongoDB won't complain unless you explicitly enforce uniqueness with a unique index (see the Indexing section).
Insert an Array
Wrap values in square brackets [ ] to store a list under one field — the array can hold any mix of data types.
db.emp.insertOne({
"name": "Roshan",
"email": "roshan@gmail.com",
"marks": [100, 200, 300, 400]
})
Insert an Embedded Object
Use curly braces { } with key-value pairs to nest a document inside another document.
db.emp.insertOne({
"name": "Rahit",
"email": "rohit@gmail.com",
"reporting": {
"managerid": 121,
"managername": "shubham",
"managerdesignation": "Sr Software Engineer"
}
})
insertMany()
Pass an array of documents to insert several at once in a single call.
db.emp.insertMany([
{ "name": "mangesh", "email": "mangesh@gmail.com" },
{ "name": "hitesh", "email": "hitesh@gmail.com" },
{ "name": "harshit", "email": "harshit@gmail.com" }
])
// {
// acknowledged: true,
// insertedIds: { '0': ObjectId(...), '1': ObjectId(...), '2': ObjectId(...) }
// }
Enforcing Uniqueness with createIndex()
If duplicate values (like the email above) are a problem, create a unique index on that field:
db.emp.createIndex({ email: 1 }, { unique: true })
If duplicates already exist, MongoDB refuses to build the index until you remove them:
E11000 duplicate key error collection: rahultechnology.emp index: email_1
dup key: { email: "rahul@gmail.com" }
Once the duplicates are gone and the index is created, any future insert with a repeated email fails immediately:
db.emp.insertOne({ "name": "Rahil", "email": "rahul@gmail.com" })
// MongoServerError: E11000 duplicate key error ... dup key: { email: "rahul@gmail.com" }
Common Errors & Troubleshooting
E11000 duplicate key error— you tried to insert a value that violates a unique index (like a repeated email oncecreateIndex({email:1},{unique:true})is in place). Check the existing documents for the conflicting value, or useupdateOne(..., {upsert:true})instead if you meant to update.insertMany()stops partway through on an error — by default,insertMany()halts on the first failed document (like a duplicate key) and doesn't insert the rest. Pass{ ordered: false }if you want it to skip failures and keep inserting the remaining documents.- Forgetting quotes around field names with special characters — a field name containing a dot or dollar sign (like
"price.usd") needs to be quoted; MongoDB reserves unquoted dots for addressing nested fields.
Key Takeaways
insertOne()inserts a single document;insertMany()inserts an array of them.- Arrays use
[ ], embedded documents use{ }— both can be inserted directly. - MongoDB allows duplicate field values by default — use
createIndex({ field: 1 }, { unique: true })to prevent them.