find() का उपयोग
find एक collection के सारे documents return करता है, या वे जो एक filter match करते हैं।
In this page:
Syntax
db.collection_name.find(filter, projection)
db.collection_name.find({ field: value })
find()
db.collection.find(filter, projection) matching documents पर एक cursor return करता है। एक empty या missing filter सब कुछ match करता है। mongosh में पहले 20 results automatically print होते हैं, और ज़्यादा देखने के लिए आप it type कर सकते हैं।
Note:
बिना arguments के find सब कुछ return करता है, इसलिए बड़ी collections पर एक filter add करें।
उदाहरण: find()
test> db.users.insertMany([
... { _id: 1, name: "Ada", age: 36, city: "London" },
... { _id: 2, name: "Bob", age: 25, city: "Oslo" },
... { _id: 3, name: "Cy", age: 41, city: "Oslo" }
... ])
{ acknowledged: true, insertedIds: { '0': 1, '1': 2, '2': 3 } }
test> db.users.find()
[
{ _id: 1, name: 'Ada', age: 36, city: 'London' },
{ _id: 2, name: 'Bob', age: 25, city: 'Oslo' },
{ _id: 3, name: 'Cy', age: 41, city: 'Oslo' }
]
test> db.users.find({ city: "Oslo" })
[
{ _id: 2, name: 'Bob', age: 25, city: 'Oslo' },
{ _id: 3, name: 'Cy', age: 41, 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. #}
आम गलतियां
- यह भूल जाना कि find एक cursor return करता है, array नहीं
- बिना filter या limit के huge collections पर find चलाना
- collection name misspell करना और एक empty result पाना
चैप्टर सारांश
- find(filter, projection) एक cursor return करता है
- Empty filter सब कुछ match करता है
- mongosh पहले 20 print करता है
- बड़े data पर filters और limits add करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: