How could I get the matched nested items in array
I want to return the matched items in nested array.
For example, I want to filter out the records that contain "A428 ","A429 "
in their items
How could I get it ?
Query
pipeline_work = [
{ '$match': 'records.items': '$in': ["A428 ","A429 "])}
]
cur = db[source_collection].runCommand('aggregate',pipeline: pipeline_work , allowDiskUse: true)
Sample Document
{
"_id": "0007db2dac8d6482ec60c228b700c3ec",
"records": [
{
"APPL_DATE": new Date("1996-03-19T08:00:00+0800"),
"FUNC_DATE": new Date("1996-02-27T08:00:00+0800"),
"items": [
"A428 ",
" ",
" "
]
},
{
"APPL_DATE": new Date("1996-03-19T08:00:00+0800"),
"FUNC_DATE": new Date("1996-02-27T08:00:00+0800"),
"items": [
"A429 ",
" ",
" "
]
},
{
"APPL_DATE": new Date("1996-04-15T08:00:00+0800"),
"FUNC_DATE": new Date("1996-03-18T08:00:00+0800"),
"items": [
"A180 ",
" ",
" "
]
}]
Expected result
"_id": "0007db2dac8d6482ec60c228b700c3ec",
"records": [
{
"APPL_DATE": new Date("1996-03-19T08:00:00+0800"),
"FUNC_DATE": new Date("1996-02-27T08:00:00+0800"),
"items": [
"A428 ",
" ",
" "
]
},
{
"APPL_DATE": new Date("1996-03-19T08:00:00+0800"),
"FUNC_DATE": new Date("1996-02-27T08:00:00+0800"),
"items": [
"A429 ",
" ",
" "
]
}]
Failed with {"errmsg":"exception: A pipeline stage specification object must contain exactly one field.","code":16435,"ok":0}
pipeline_work = [
{
"$unwind": "$records",
"$match": {
"records.items": {
"$in": ["A428 ", "A429 "]
}
},
"$group": {
"_id": "$_id",
"records": {
"$push": "$records"
}
}
}, {
'$limit': 1
}
];