Documents और Collections
एक document fields से बना एक record है, और एक collection documents का एक group है, records की एक table की तरह।
In this page:
Syntax
db.collection_name.insertOne({
field: "value",
nested: { key: value },
list: [value1, value2]
})
Documents और Collections
Documents field-value pairs हैं जो strings, numbers, booleans, dates, arrays, और nested documents hold कर सकते हैं। एक collection related documents group करती है और पहले insert पर automatically बनती है।
सame collection में documents का same fields होना ज़रूरी नहीं।
Note:
Collections default से schemaless हैं, लेकिन आप validation rules add कर सकते हैं।
उदाहरण: Documents & Collections
test> db.people.insertMany([
... { _id: 1, name: "Ada", address: { city: "London", zip: "N1" }, skills: ["math", "code"] },
... { _id: 2, name: "Linus", email: "[email protected]" }
... ])
{ acknowledged: true, insertedIds: { '0': 1, '1': 2 } }
test> db.people.find()
[
{
_id: 1,
name: 'Ada',
address: { city: 'London', zip: 'N1' },
skills: [ 'math', 'code' ]
},
{ _id: 2, name: 'Linus', email: '[email protected]' }
]
test> db.people.findOne({ "address.city": "London" }, { name: 1, "address.zip": 1 })
{ _id: 1, name: 'Ada', address: { zip: 'N1' } }
⚠️ 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. #}
आम गलतियां
- यह उम्मीद करना कि सारे documents के identical fields होंगे
- सब कुछ के लिए एक giant collection बनाना
- यह भूल जाना कि collection names case sensitive हैं
चैप्टर सारांश
- एक document fields और values का एक set है
- Values arrays और nested documents हो सकती हैं
- एक collection documents group करती है
- Collections पहले insert पर बनती हैं
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: