$ का उपयोग करना
स्थितीय ऑपरेटर, आप परिणाम प्राप्त कर सकते हैं। हालाँकि, यदि आपके पास vehicles . में एक से अधिक तत्व हैं सरणी उन सभी को परिणाम में लौटा दी जाएगी, क्योंकि आप प्रक्षेपण में केवल एक स्थितीय ऑपरेटर का उपयोग कर सकते हैं और आप 2 सरणियों (एक दूसरे के अंदर) के साथ काम कर रहे हैं।
मेरा सुझाव है कि आप aggregation framework पर एक नज़र डालें।
, क्योंकि आपको बहुत अधिक लचीलापन मिलेगा। शेल में चलने वाले आपके प्रश्न के लिए यहां एक उदाहरण क्वेरी है। मैं नेवले से परिचित नहीं हूँ, लेकिन मुझे लगता है कि यह अभी भी आपकी मदद करेगा और आप इसका अनुवाद करने में सक्षम होंगे:
db.collection.aggregate([
// Get only the documents where "email" equals "example@sqldat.com" -- REPLACE with params.username
{"$match" : {email : "example@sqldat.com"}},
// Unwind the "inventories" array
{"$unwind" : "$inventories"},
// Get only elements where "inventories.title" equals "activeInventory"
{"$match" : {"inventories.title":"activeInventory"}},
// Unwind the "vehicles" array
{"$unwind" : "$inventories.vehicles"},
// Filter by vehicle ID -- REPLACE with vehicleID
{"$match" : {"inventories.vehicles._id":ObjectId("53440e94c02b3cae81eb0069")}},
// Tidy up the output
{"$project" : {_id:0, vehicle:"$inventories.vehicles"}}
])
यह वह आउटपुट है जो आपको मिलेगा:
{
"result" : [
{
"vehicle" : {
"_id" : ObjectId("53440e94c02b3cae81eb0069"),
"tags" : [
"vehicle"
],
"details" : [
{
"_id" : ObjectId("53440e94c02b3cae81eb0066"),
"year" : 2007,
"transmission" : "Manual",
"price" : 1000,
"model" : "Firecar",
"mileageReading" : 50000,
"make" : "Bentley",
"interiorColor" : "blue",
"history" : "CarProof",
"exteriorColor" : "blue",
"driveTrain" : "SWD",
"description" : "test vehicle",
"cylinders" : 4,
"mileageType" : "kms"
}
]
}
}
],
"ok" : 1
}