MongoDB query check if value in array property [duplicate]
Asked Answered
Y

3

7

I cannot find out of how to check, if some value are in array property in mongo document. For example, I have some collection users, and such document:

{
  'name':'Paul',
  'age':43,
  'friendsIDs': [ ObjectId('qqq...'), ObjectId('www...'), ObjectId('eee...') ],
}

Now let's suppose that I want to check, is user with ID ObjectId('qqq...') a friend of Paul, or not. This is quite easy to do in almost all programming languages, for example in php it would be something like:

$isFriendOfPaul = in_array( ObjectId('qqq...'), $friendsIds );

But how to query this in mongo? Any ideas?

Yoruba answered 15/4, 2013 at 10:10 Comment(0)
S
25

Actually, it is $in the manual.

Serif answered 15/4, 2013 at 11:20 Comment(4)
show me the query please. It's vice versa of query with $in. – Yoruba
"If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array (e.g. <value1>, <value2>, etc.)" -- so it should work both ways. So I guess you could try to get it: db.collection.find({friendsIDs: {$in: [YourIDValue]}}) – Serif
Nobody even addresses the pun that made me fall over? – Burseraceous
badumts tching πŸ₯πŸ˜Ž – Unbreathed
F
3

Your query must be like this:

db.collection.find({"name": "Paul", "friendsIDs": "qqq"}) 

This query finds the document with name Paul and friendsIDs equal to qqq.

Flaxen answered 15/4, 2013 at 10:23 Comment(3)
'friendsIDs' is a list (array) of Ids, not one ID – Yoruba
This works fine even when friendsIDs is an array. – Licentious
docs.mongodb.com/manual/reference/operator/query/eq/… – Amidships
H
1

This can also be done with $eq

> db.person.insert({name: 'Paul', age: 43, friends: [1234, 2345, 3456]})
> db.person.insert({name: 'Dave', age: 23, friends: [2345, 3456]})
> db.person.insert({name: 'Stephen', age: 12, friends: [2345, 3456, 7890]})

Below is an example using $eq:

> db.person.find({friends: {$eq : 2345}}) 
{ "_id" : ObjectId("54e34124fe135475834334da"), "name" : "Paul", "age" : 43, "friends" : [ 1234, 2345, 3456 ] }
{ "_id" : ObjectId("54e34138fe135475834334db"), "name" : "Dave", "age" : 23, "friends" : [ 2345, 3456 ] }
{ "_id" : ObjectId("54e3414bfe135475834334dc"), "name" : "Stephen", "age" : 12, "friends" : [ 2345, 3456, 7890 ] }

but the queries below also find the same. So as Sven suggested, you can leave out $eq.

> db.person.find({friends: 2345})
{ "_id" : ObjectId("54e34124fe135475834334da"), "name" : "Paul", "age" : 43, "friends" : [ 1234, 2345, 3456 ] }
{ "_id" : ObjectId("54e34138fe135475834334db"), "name" : "Dave", "age" : 23, "friends" : [ 2345, 3456 ] }
{ "_id" : ObjectId("54e3414bfe135475834334dc"), "name" : "Stephen", "age" : 12, "friends" : [ 2345, 3456, 7890 ] }

> db.person.find({friends: 1234})
{ "_id" : ObjectId("54e34124fe135475834334da"), "name" : "Paul", "age" : 43, "friends" : [ 1234, 2345, 3456 ] }
Haywire answered 17/2, 2015 at 13:40 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.