Here is one of the document from which i would like get all the child.item ('Toy1','Toy2' etc) in a list.
{
_id: 'Toy',
name: 'Toyname',
child: [
{ item: 'Toy1', isActive: '1', Type: 'New' },
{ item: 'Toy2', isActive: '2', Type: 'Old' }
]
}
I have tried
pipeline = [
{
"$match": {
"_id": "Toy",
"child.isActive": '1'
}
},
{
"$unwind":
"$child"
},
]
list(DB.Category.aggregate(pipeline))
[{'_id': 'Toy', 'name': 'Toyname', 'child': {'item': 'Toy1', 'isActive': 1, 'Type': 'New'}}, {'_id': 'Toy', 'name': 'Toyname', 'child': {'item': 'Toy2', 'isActive': 2,
'Type': 'Old'}}]
And tried this on dbshell
#> db.Category.find({name:"Toyname",child:{$elemMatch:{Type:"New"} }})
[
{
_id: 'Toy',
name: 'Toyname',
child: [
{ item: 'Toy1', isActive: '1', Type: 'New' },
{ item: 'Toy2', isActive: '2', Type: 'Old' }
]
}
]
But as you can see it gives me both the items from the array. I am using pymongo 4.0.1.
Expecting a list as o/p which fulfilled match criteria like Type:'New' in $match should return 'Toy1' and isActive:'2' should return 'Toy2'. I hope i am able to put my requirement clearly.