Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

arr=[{field1:<value1>,field2:<value2}.....]

I want to use the $in operator against the field1 of arr. I know I could create a dummy array and push the field1 values in the dummy array. But is there a way to use the $in operator against a particular field of an array?

The arr array is no way related to the collection.

I want to query all the documents on a particular field whose value is in the field1 of arr - field1 should be in the right hand side of operator $in

Example:

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

db.collection.find({fieldx:<Here I want some method to obtain all documents whose fieldx values are in the location field of arr>})

The output should contain documents whose fieldx values are present in the location field of arr.

The ouput of the query should be

[{... 
    fieldx:'NY',
...
},
{...
    fieldx:'LA',
...
},
{...
    fieldx:'NZ',
...
}]

fieldx is the field in the collection I am querying on. It is not a field of the array I'm providing(arr). I want to match this to a field(location) of array - arr I'm providing the query.

like image 640
ma08 Avatar asked Jul 01 '14 05:07

ma08


People also ask

How do you get a specific object from an array in MongoDB?

To search the array of object in MongoDB, you can use $elemMatch operator. This operator allows us to search for more than one component from an array object.

How do I query an element in an array in MongoDB?

To query if the array field contains at least one element with the specified value, use the filter { <field>: <value> } where <value> is the element value. To specify conditions on the elements in the array field, use query operators in the query filter document: { <array field>: { <operator1>: <value1>, ... } }

Which operator selects the document where the value of a field equals any value in the specified array The?

The $in operator choose the documents where the value of a field equals any value in the specified array. Syntax: { filed: { $in: [ <value1>, <value2>, ……] } }


1 Answers

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.

like image 137
Neil Lunn Avatar answered Nov 15 '22 21:11

Neil Lunn