Projection क्या है
Projection चुनता है कि कौन-से fields वापस आएँ ताकि आप सिर्फ वही transfer करें जो आपको चाहिए।
In this page:
Syntax
db.collection_name.find(filter, { field1: 1, field2: 1, _id: 0 })
Projection
find का दूसरा argument include करने के लिए 1 या exclude करने के लिए 0 से fields list करता है। आप _id को छोड़कर inclusion और exclusion mix नहीं कर सकते, जो default से included है जब तक इसे 0 set न किया जाए। Dotted paths nested fields project करते हैं, और $slice arrays trim करता है।
Note:
जब आपको _id नहीं चाहिए हमेशा इसे explicitly exclude करें।
उदाहरण: Projection
test> db.users.insertOne({ _id: 1, name: "Ada", age: 36, address: { city: "London", zip: "N1" }, skills: ["a", "b", "c"] })
{ acknowledged: true, insertedId: 1 }
test> db.users.find({}, { name: 1 })
[ { _id: 1, name: 'Ada' } ]
test> db.users.find({}, { name: 1, _id: 0, "address.city": 1 })
[ { name: 'Ada', address: { city: 'London' } } ]
test> db.users.find({}, { age: 0, address: 0 })
[ { _id: 1, name: 'Ada', skills: [ 'a', 'b', 'c' ] } ]
⚠️ 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. #}
आम गलतियां
- Include और exclude mix करना
- यह भूल जाना कि _id default से return होता है
- जब दो fields काफी हों पूरे documents fetch करना
चैप्टर सारांश
- 1 include करता है और 0 exclude करता है
- _id को छोड़कर mix न करें
- _id default से included है
- Dotted paths nested fields project करते हैं
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: