← Back to MongoDB Course | Chapter 1: Introduction | Lesson 3 of 7

Documents और Collections

एक document fields से बना एक record है, और एक collection documents का एक group है, records की एक table की तरह।
Syntax
javascript
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

bash
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. #}
आम गलतियां
  1. यह उम्मीद करना कि सारे documents के identical fields होंगे
  2. सब कुछ के लिए एक giant collection बनाना
  3. यह भूल जाना कि 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.