Adding data to a collection is done with insertOne() for a single document, or insertMany() for several at once.
db.users.insertOne({
name: "Ananya Roy",
email: "ananya@example.com",
age: 27
})The response includes the generated _id of the new document, confirming it was created:
{
acknowledged: true,
insertedId: ObjectId("651f2b1a1c4a2b0012a4f8d2")
}Pass an array of documents to insert several in a single call — this is meaningfully faster than calling insertOne() in a loop, since it is one round trip to the database instead of many:
db.users.insertMany([
{ name: "Karan Mehta", age: 22 },
{ name: "Sneha Patel", age: 30 },
{ name: "Vikram Singh", age: 25 }
])You can set _id explicitly instead of letting MongoDB generate one — useful when you want a predictable, human-meaningful identifier. MongoDB will reject the insert if the value is already in use in that collection:
db.users.insertOne({
_id: "user-ananya",
name: "Ananya Roy"
})_id is immutable
Once set, a document's _id cannot be changed — trying to update() the _id field throws an error. If you need a different identifier later, you must delete and re-insert the document.