insertOne() का प्रयोग
insertOne एक collection में एक single document add करता है और आपको नया _id बताता है।
In this page:
Syntax
db.collection_name.insertOne({ field1: value1, field2: value2 })
insertOne()
db.collection.insertOne(document) एक document insert करता है, ज़रूरत पड़ने पर collection बनाते हुए।
अगर आप एक _id supply नहीं करते, MongoDB एक ObjectId generate करता है। Result acknowledged और insertedId report करता है।
एक duplicate _id insert करना एक duplicate key error raise करता है।
Note:
जब आपके पास एक natural unique key हो अपना खुद का _id supply करें।
उदाहरण: insertOne()
test> db.users.insertOne({ _id: 1, name: "Ada", age: 36 })
{ acknowledged: true, insertedId: 1 }
test> db.users.insertOne({ _id: 2, name: "Bob", age: 25, city: "Oslo" })
{ acknowledged: true, insertedId: 2 }
test> db.users.find()
[
{ _id: 1, name: 'Ada', age: 36 },
{ _id: 2, name: 'Bob', age: 25, city: 'Oslo' }
]
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- यह उम्मीद करना कि insertOne एक existing document update करेगा
- यह भूल जाना कि collection automatically बनती है
- duplicate key errors ignore करना
चैप्टर सारांश
- insertOne एक document add करता है
- अगर missing हो तो _id generate होता है
- Result में acknowledged और insertedId है
- Duplicate _id एक error raise करता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: