ऐरे में मिले केवल एक एलिमेंट को डिस्प्ले करने के लिए aggregate() का उपयोग करें। पहले कलेक्शन बनाएं:
> db.demo204.insertOne({
"_id": 101,
"Name": "Chris",
"Age": 23,
"details": [
{ "id": "1001", "empId": "John_1001", "salary": "50000", "Technology": "Java" },
{ "id": "1002", "empId": "John_1002" },
{ "id": "1003", "empId": "John_10003", "salary": "60000", "Technology": "MongoDB" }
]
});
{ "acknowledged": true, "insertedId": 101 }
सभी डॉक्यूमेंट्स देखें:
> db.demo204.find();
आउटपुट:
{ "_id": 101, "Name": "Chris", "Age": 23, "details": [
{ "id": "1001", "empId": "John_1001", "salary": "50000", "Technology": "Java" },
{ "id": "1002", "empId": "John_1002" },
{ "id": "1003", "empId": "John_10003", "salary": "60000", "Technology": "MongoDB" }
] }
ऐरे में मिले एलिमेंट को डिस्प्ले करने की क्वेरी:
> db.demo204.aggregate([
{ "$match": { "details.id": "1001" }},
{ "$unwind": "$details" },
{ "$match": { "details.id": "1001" }},
{ "$project": { "Technology": "$details.Technology", "_id": 0 }}
])
परिणाम:
{ "Technology": "Java" }