MongoDB query check if value in array property

Void Floyd picture Void Floyd · Apr 15, 2013 · Viewed 16.5k times · Source

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?

Answer

guessimtoolate picture guessimtoolate · Apr 15, 2013

Actually, it is $in the manual.