Mongodb $in against a field of objects of array instead of objects of array

前端 未结 3 1986
北恋
北恋 2021-02-19 13:22
arr=[{field1:,field2:

I want to use the $in operator against the field1 of arr. I

3条回答
  •  忘掉有多难
    2021-02-19 13:38

    Extending Neil Lunn's answers, you can use map function within query also.

    var arr = [
        { "name": "foo", "location": "NY"},
        { "name": "bar", "location": "LA"},
        { "name": "foobar", "location": "NZ"}
    ];
    
    db.collection.find({ "fieldx": { "$in": arr.map(function(x) { return x.location } ) } })
    

    If you using ES6 syntax then

    db.collection.find({ "fieldx": { "$in": arr.map(x => x.location ) } })
    

提交回复
热议问题