Kavin Varnan
Kavin Varnan

Reputation: 1989

Find query in mongodb from specified array

I have two JSON document on a mongo collection

{"_id" : ObjectId("asdf"), "employee" : {"name": "Bob"}}
{"_id" : ObjectId("1234"), "employee" : {"name": "Rob"}}

All i just wanted to make a simple query get the name of the employee from my array.

I tried

db.myCollection.find({"employee.name": "Bob"})

This works and returns only 1 document.

db.myCollection.find({"employee.name": ["Bob", "Rob"]})

Doesnt work. Doesnt return anything

db.myCollection.find({"employee.name": "Bob", "Rob"})

Syntax error

db.myCollection.find({"employee.name": {$all : ["Bob", "Rob"]} })

Doesnt work. Doesnt return anything

I know this is a very simple query. But couldn't figure out the syntax.

Upvotes: 1

Views: 118

Answers (3)

Alex
Alex

Reputation: 21766

You need to use the $in operator, see the documentation

db.myCollection.find({"employee.name":{$in:["Bob","Rob"]}} )

The $all operator doesn't return any documents, as it selects the documents where the value of a field is an array that contains all the specified elements. The following record would satisfy this condition:

{"_id" : ObjectId("asdf"), "employee" : {"name": ["Bob","Rob"]}}

The above record would also be returned by this query:

db.myCollection.find({"employee.name": ["Bob", "Rob"]})

Finally, the query below throws an invalid syntax error as {"employee.name": "Bob", "Rob"} is not a valid JSON\BSON document:

db.myCollection.find({"employee.name": "Bob", "Rob"})

Upvotes: 1

Ravi Rupeliya
Ravi Rupeliya

Reputation: 621

Try using $in operator

query : db.myCollection.find({"employee.name": { $in: ["Bob", "Rob"] }})

For the options you tried,

db.myCollection.find({"employee.name": "Bob"})

This will return the records with name having "Bob", so that is why it is returning only one record.

db.myCollection.find({"employee.name": ["Bob", "Rob"]})

For this option we have provided ["Bob", "Rob"] for matching value, which actually is and array, so it will return records with same array in name.

db.myCollection.find({"employee.name": "Bob", "Rob"})

For this option, as Jaco said it is not a valid json format.

db.myCollection.find({"employee.name": {$all : ["Bob", "Rob"]} })

This option will try to find records with value of name field as an array such that the value array contains all the values provided in $all

Upvotes: 2

Rubin Porwal
Rubin Porwal

Reputation: 3845

According to above mentioned description as an alternate solution to it please try executing following query assuming that all documents need to be fetched

db.getCollection('myCollection').find({},{_id:0,'employee.name':1})

The above query fetches all documents from collection utilizing projection document to retrieve specific fields in resultset

For more detailed desciption regarding MongoDB projection document please refer the following documentation as mentioned in following URL

https://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/

Upvotes: 0

Related Questions