Mongodb $ in the field of objects of array objects instead of array objects

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

I want to use the $in operator for field1 arr . I know that I can create a dummy array and push the field1 values ​​in the dummy array. But is there a way to use the $in operator for a specific field of an array?

The arr array is in no way associated with the collection.

I want to request all documents in a specific field, the value of which is in field1 of arr - field1 should be on the right side of the $in operator

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 must contain documents whose fieldx values ​​are present in the location arr field.

Query output should be

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

fieldx is the field in the collection I'm referring to. This is not an array field that I provide ( arr ). I want to map this to the field ( location ) of the array - arr I am providing a request.

+7
javascript mongodb mongoose mongodb-query aggregation-framework
source share
3 answers

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

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

For reference, I am going to rewrite your question for you:

I have a collection containing the following documents:

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

I have an input array that is defined as follows:

 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 matching the location field in the array that I have for input.

How can I do it?

I tried:

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

But this is not consistent.

+5
source share

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

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

This query will return all documents in which the arr array contains at least one subdocument with field field1 with values value1 or value11 .

Edit

As for your editing, you cannot use $ in operator in this way.

From the documentation :

The $ in operator selects documents in which the field value is equal to any value in the specified array.

If you send an array of objects to the $ in request, it will correspond to the document in which the specified field contains this object:

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

You will find the following documents:

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

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

+1
source share

Extending Neil Lunn's answers, you can also use the map function in the request.

 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 use ES6 syntax, then

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

All Articles