MongoDB Fetch Records | MongoDB Select Records

📔 : MongoDB 🔗

पिछले topic में आपने Collection (Table) में documents को insert करना सीखा , इस topic में हम collection से records / documents को fetch करना सीखेंगे।

MongoDB में documents को fetch करने के दो तरीके है -

  • find() : multiple records fetch करने के लिए।

  • findOne() : और single record select करने के लिए।

MongoDB find() In Hindi

find() function का use करके आप एक साथ multiple records को select कर सकते हैं , बैसे तो इस function में filter object भी pass होता है , हालाँकि अगर आप कोई भी argument pass नहीं करते तो Collection में available सभी records select हो जायेंगे।

Example के लिए हमारे पास एक users Collection है।

Switch database

use mydatabase

Fetch doucments

db.users.find()

ऊपर दी गयी query run करने पर सभी documents list हो जायेंगे।

MongoDB findOne() In Hindi

इसके नाम से ही समझ सकते हैं कि findOne() function Collection से first occurrence वाला एक ही document return करेगा।

db.users.findOne()

find based on condition

जैसा कि अभी आपने पढ़ा कि अगर आपको documents को किसी condition के base पर search करना चाहते हैं तो find() या findOne() function में condition object pass कर सकते हैं।

db.users.find({"name" : "Raju"})

Output

/* 1 */
{
    "_id" : ObjectId("64f5575e4984b45231af1b05"),
    "name" : "Raju",
    "location" : "India",
    "age" : 33.0
}

इसी तरह से आप बाकी conditions pass कर सकते हैं , हालाँकि MongoDB में कई सारे filter options हैं जिन्हे हम आगे पढ़ेंगे।

MongoDB Projection

MongoDB में Projection का मतलब है Collection से particular fields को select करना है।

Collection में available document के particular fields को select करने के लिए find() या findOne() function में 2nd argument में दूसरा object pass करते हैं।

जो fields चाहिए उन्हें उनके name के साथ 1 assign करते हैं।

db.users.find({}, {"name" : 1})

Output

{
    "_id" : ObjectId("64f5541e4984b45231af1b02"),
    "name" : "Pyare Lal"
}
{
    "_id" : ObjectId("64f5575e4984b45231af1b03"),
    "name" : "Ramu Kaka"
}

{
    "_id" : ObjectId("64f5575e4984b45231af1b04"),
    "name" : "Gabbar"
}
{
    "_id" : ObjectId("64f5575e4984b45231af1b05"),
    "name" : "Raju"
}

Example को अगर आप ध्यान से देखोगे तो हमने सिर्फ name select किया था फिर भी _id field आ रही है।

Actually MongoDB में _id by default selected रहता है , हाँ अगर आप इसे select नहीं करना चाहते हैं तो simply _id:1 pass कर सकते हैं।

db.users.find({}, {"name" : 1, _id:0})

I hope , आपको समझ आ गया होगा कि MongoDB Collection से records को कैसे fetch करते हैं , हालाँकि fetch करने के लिए कई तरह के clauses और filters हैं जिन्हे आगे समझेंगे।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers