简体   繁体   中英

find all element inside array that match specific value - mongodb

MongoDB V: 3.6

I have an array with 6 elements, each element have 2 values: fileName and count I want to get all elements in the array that have a count of 0

I have tried to use this code but it only returns one element, not all the elements that match.

await User.findOne({email:email},
                   { ackFiles: { $elemMatch: { count: 0 } } });

it only return this :

{ _id: 5b55d0fd9dc644051c801058,
  ackFiles: [ { fileName: 'terms_of_use_vad', count: 0 } ] }

this is how my DB looks like 在此处输入图片说明

db.getCollection('').aggregate([{"$match": {"email": ""}},
{"$group" : {"_id" : {"email": "$email"}}},        
{
            "$project": {
                "files": {
                    "$filter": {
                        "input": "$ackfiles",
                        "as": "file",
                        "cond": {
                            "$in": ["$$file.count", [0]]
                        }
                    }
                }
            }
        }

    ])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM