आइए दस्तावेजों के साथ एक संग्रह बनाएं -
> db.demo411.insertOne(
... {
... "Information" : [
... {
... "Name1" : "Chris",
... "Name2" : "David"
... },
... {
... "Name1" : "John",
... "Name2" : "John"
... }
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e70f19715dc524f70227682")
} संग्रह से सभी दस्तावेज़ों को खोजने () विधि की सहायता से प्रदर्शित करें -
> db.demo411.find();
यह निम्नलिखित आउटपुट देगा -
{ "_id" : ObjectId("5e70f19715dc524f70227682"), "Information" : [ { "Name1" : "Chris", "Name2" : "David" }, { "Name1" : "John", "Name2" : "John" } ] } सरणियों में केवल मान प्राप्त करने के लिए क्वेरी निम्नलिखित है -
> db.demo411.aggregate(
... [
... {$project : {
... _id : 0,
... Information : {$map : {input : "$Information", as : "out", in : ["$$out.Name1", "$$out.Name2"]}}
... }
... }
... ]
... ) यह निम्नलिखित आउटपुट देगा -
{ "Information" : [ [ "Chris", "David" ], [ "John", "John" ] ] }