Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> MongoDB

MongoDB में Array Elements के साथ Specific Document Fetch करना

MongoDB में किसी specific document को fetch करने के लिए find() मेथड में dot notation का उपयोग करें। आइए documents के साथ एक collection बनाते हैं:

> db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"Oppo"}]});
{ "acknowledged" : true, "insertedId" : ObjectId("5ea3ea9b04263e90dac943e5") }
> db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"Samsung"}]});
{ "acknowledged" : true, "insertedId" : ObjectId("5ea3eaa404263e90dac943e6") }
> db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"OnePlus"}]});
{ "acknowledged" : true, "insertedId" : ObjectId("5ea3eacc04263e90dac943e7") }

सभी Documents Display करना

find() मेथड की मदद से collection के सभी documents display करें:

> db.demo672.find();

इससे निम्न output प्राप्त होगा:

{ "_id" : ObjectId("5ea3ea9b04263e90dac943e5"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Oppo" } ] }
{ "_id" : ObjectId("5ea3eaa404263e90dac943e6"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Samsung" } ] }
{ "_id" : ObjectId("5ea3eacc04263e90dac943e7"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "OnePlus" } ] }

Specific Document Fetch करने का Query

Array element के आधार पर specific document fetch करने के लिए dot notation का उपयोग करें:

> db.demo672.find({"Brand.Name":"OnePlus"});

Output:

{ "_id" : ObjectId("5ea3eacc04263e90dac943e7"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "OnePlus" } ] }

नोट: Dot notation (Brand.Name) का उपयोग करके आप nested array elements को directly query कर सकते हैं। यह MongoDB की powerful feature है जो complex data structures को query करने में मदद करती है।

  1. - MongoDB Aggregation - Specific Field Value वाले Documents Fetch करना

    इसके लिए aggregate() का उपयोग करें। मान लें हमें field Age value 21 वाले documents fetch करने हैं। Collection बनाना > db.demo685.insertOne( ... { ... details: ... [ ... { Name:Chris, Age:21 }, ... { Name:David, Age:23 }, ... { Name:Bob, Age:21 } ... ] ... } ...

  1. - MongoDB में एम्बेडेड डॉक्यूमेंट की सरणी पर फ़िल्टर क्वेरी कैसे करें?

    इसके लिए MongoDB में aggregate() का उपयोग करें। आइए 3 स्तरों के नेस्टेड डॉक्यूमेंट्स के साथ एक कलेक्शन बनाते हैं: > db.demo736.insertOne( { _id: 101, details1: [ { details2: [ { details3: { Name: John } } ] } ] } ); { acknowledged : true, insertedId : 101 } > db.demo736.insertOne( { _id: 102,

  1. - MongoDB में सरणी क्वेरी करके विशिष्ट मान प्राप्त करें

    सरणी से विशिष्ट मान प्राप्त करने के लिए aggregate() के साथ $project का उपयोग करें। आइए एक कलेक्शन बनाते हैं: > db.demo761.insertOne( { details: [ { student: { FullName: Chris Brown } } ] } ); { acknowledged : true, insertedId : ObjectId(5eb034d15637cd592b2a4af1) } > db.demo761.insertOne( { de