MongoDB mongosh Insert
MongoDB mongosh – Insert Documents 📝
MongoDB provides simple and powerful insert methods in mongosh.
1️⃣ Insert a Single Document (insertOne)
Syntax
Example
Output
📌 If the collection does not exist, MongoDB creates it automatically.
2️⃣ Insert Multiple Documents (insertMany)
Syntax
Example
Output
3️⃣ Insert with Custom _id
MongoDB automatically creates _id, but you can define it yourself.
⚠️ _id must be unique.
4️⃣ Insert Nested (Embedded) Documents
5️⃣ Verify Inserted Data
6️⃣ Insert with Write Concern (Basic)
7️⃣ Common Insert Errors ⚠️
Duplicate _id Error
Invalid Document
-
Circular references
-
Unsupported data types
SQL vs MongoDB (INSERT)
SQL
MongoDB
