This is my data.
{
"applianceType": "light",
"applianceId": "light123",
"applianceName": "Light",
"applianceImgURL": "https://s3-us-west-2.amazonaws.com/app.tiktok/appliance/icons/lights.svg",
"inUsed": false,
"collection": [{
"category": "ceiling_light",
"catgName": "Ceiling light",
"catgImgURL": "https://s3-us-west-2.amazonaws.com/app.tiktok/appliance/catg_thumbs/light/ceiling_light.jpg",
"collectionCatgId": "LI-ceil",
"items": [{
"id": "LI-CHL1234",
"name": "Chandelier",
"imgURL": "https://s3-us-west-2.amazonaws.com/app.tiktok/appliance/thumbs/light/chandelier.jpg",
"capImgURL": "",
"inUsed": false,
"editable": true,
"quantity": {
"number": 1,
"multiple": false
},
"value": {
"state": "OFF",
"range": {
"minVal": "0",
"currentVal": "20",
"maxVal": "100"
}
},
"rooms": []
}, {
"id": "LI-PL1234",
"name": "Pendant light",
"imgURL": "https://s3-us-west-2.amazonaws.com/app.tiktok/appliance/thumbs/light/pendantlight.jpg",
"capImgURL": "",
"inUsed": false,
"editable": true,
"quantity": {
"number": 1,
"multiple": false
},
"value": {
"state": "OFF",
"range": {
"minVal": "0",
"currentVal": "20",
"maxVal": "100"
}
},
"rooms": []
}]
}]
}
All I want is to get a particular document of collection.items
I have tried this query so far :
db.myappliance.find(
{
"applianceType": "light"
},
{
"collection" : {
$elemMatch: {
"category" : "ceiling_light",
"items": {
$elemMatch: {
"id": "LI-CHL1234"
}
}
}
}
}
)
This query gives me all the documents inside collection.items array. But I want only one. I know that my question has many duplicate thread already. But I couldn't find any of those answer fit into my case.
There's no way to do that on
query.After you get the result from the query on the mongodb collections, use that to retrieve the wanted object.