insertOne() — MongoDB Technical Interview Questions & Answers
Inserts a single document into a collection.
insertOne()
- Simple meaning: insertOne adds one document. Mongo adds _id if you omit it.
- How to use: db.students.insertOne({ name: "Asha", roll: 12, city: "Pune", marks: 72 })
- Result: { acknowledged: true, insertedId: ObjectId("...") }.
- Try this: insert Asha, then db.students.find().
- Common mistake: insertOne([ {...}, {...} ]) — that is insertMany. Or forgetting quotes on keys in some drivers.
- Interview tip: One doc, auto _id, returns insertedId.
Technical Interview Tip: Show insertOne with name, roll, marks.
Code Examples for insertOne()
insertOne() — working example
db.users.insertOne({
name: "Ada",
email: "ada@example.com",
age: 36,
city: "Pune",
skills: ["Java", "MongoDB"],
active: true
})insertOne() — insertOne adds a single BSON document; MongoDB assigns ObjectId _id if missing.
How to Explain insertOne() to an Interviewer
What does insertOne return?
acknowledged and insertedId