एकाधिक सरणियों की एक सरणी बनाने के लिए, MongoDB समुच्चय में $unwind का उपयोग करें। आइए दस्तावेजों के साथ एक संग्रह बनाएं -
> db.demo289.insertOne({"Section":["A","B","E"],"Name":"Chris"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e4c06fcf49383b52759cbc3")
}
> db.demo289.insertOne({"Section":["C","D","B"],"Name":"David"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e4c070af49383b52759cbc4")
} संग्रह से सभी दस्तावेज़ों को खोजने () विधि की सहायता से प्रदर्शित करें -
> db.demo289.find().pretty();
यह निम्नलिखित आउटपुट देगा -
{
"_id" : ObjectId("5e4c06fcf49383b52759cbc3"),
"Section" : [ "A", "B", "E" ],
"Name" : "Chris"
}
{
"_id" : ObjectId("5e4c070af49383b52759cbc4"),
"Section" : [ "C", "D", "B" ],
"Name" : "David"
} MongoDB में एकाधिक सरणियों की एकल सरणी बनाने की क्वेरी निम्नलिखित है -
> db.demo289.aggregate({ $unwind : "$Section" } ); यह निम्नलिखित आउटपुट देगा -
{ "_id" : ObjectId("5e4c06fcf49383b52759cbc3"), "Section" : "A", "Name" : "Chris" }
{ "_id" : ObjectId("5e4c06fcf49383b52759cbc3"), "Section" : "B", "Name" : "Chris" }
{ "_id" : ObjectId("5e4c06fcf49383b52759cbc3"), "Section" : "E", "Name" : "Chris" }
{ "_id" : ObjectId("5e4c070af49383b52759cbc4"), "Section" : "C", "Name" : "David" }
{ "_id" : ObjectId("5e4c070af49383b52759cbc4"), "Section" : "D", "Name" : "David" }
{ "_id" : ObjectId("5e4c070af49383b52759cbc4"), "Section" : "B", "Name" : "David" }