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

前端 未结 3 1937
北恋
北恋 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 ) } })
    
    0 讨论(0)
  • 2021-02-19 13:42

    You need to use dot notation to select fields in the array:

    db.coll.find({"arr.field1" : { $in : [ 'value1', 'value11' ]}});
    

    This query will return all documents where array arr contains at least one subdocument that has a field field1 with values value1 or value11.

    Edit

    Regarding your edit, you can't use $in operator that way.

    From the documentation:

    The $in operator selects the documents where the value of a field equals any value in the specified array.

    If you send an array of objects in the $in query it will match document where specified field contains that object:

    db.coll.find({ a: { $in : [ { b : 1, c: 1}, { b : 2, c: 2}  ]}});
    

    Will find this documents:

    { 
        _id : ...
        a: { b : 1, c: 1} 
    }
    { 
        _id : ...
        a: { b : 2, c: 2} 
    }
    

    To get what you really want, easiest way to query is to extract the values from your array of objects and do a query with the created array of values.

    0 讨论(0)
  • 2021-02-19 13:44

    You need to extract the "location" fields from your input array and feed them to $in:

    var locs = arr.map(function(x) { return x.location } );
    db.collection.find({ "fieldx": { "$in": locs } })
    

    For reference here I'm going to re-write your question for you:

    I have a collection that contains documents like this:

    { "fieldx": "NY" }
    { "fieldx": "LA" }
    { "fieldx": "SF" }
    

    What I have is an input array that is defined like this:

    var arr = [
        { "name": "foo", "location": "NY"},
        { "name": "bar", "location": "LA"},
        { "name": "foobar", "location": "NZ"}
    ];
    

    Now I want to write a query to find all the documents that match the "location" field in the array I have for input.

    How do I do I do that?

    I have tried:

    db.collection.find({ "fieldx": { "$in": arr } })
    

    But that does not match.

    0 讨论(0)
提交回复
热议问题