7

I need to retrieve documents that contain at least one value inside an array. The structure of my document is:

{ "_id": 3, 
"username": "111111", 
"name": "XPTO 1", 
"codes": [ 2, 4, 5 ], 
"available": true }


{ "_id": 4, 
"username": "22222", 
"name": "XPTO 2", 
"codes": [ 3, 5 ], 
"available": true }

I need to do a find by "codes" and if i search for value "5", i need to retrieve all documents that contains this value inside their array.

I've tried to use #elemMatch but no success...

db.user.find({codes: {"$elemMatch": {codes: [2,8]}}}, {"codes":1})

How can i do this?

Thanks in advance.

1
  • You can use the $in operator for this, db.user.find({code: {$in: [2,8]}}) But it will not search for where those two values exist in a single document only for documents that have a value 2 or 8 or both Commented Jan 16, 2013 at 19:58

1 Answer 1

14

You can check for values inside an array just like you compare the values for some field. So, you would need to do it like this, without using $elemMatch: -

  1. If you want to check whether an array contain a single value 5: -

    db.user.find({codes: 5}, {codes:1})
    

    This will return all the document, where codes array contain 5.

  2. If you want to check whether an array contain a value out of given set of values: -

    db.user.find({codes: {$in: [2, 8]}}, {codes:1})
    

    This will return documents with array containing either 2 or 8

  3. If you want to check whether an array contain all the values in a list: -

    db.user.find({codes: {$all: [2, 5]}}, {codes:1})
    

    This will return all document with array containing both 2 and 5.

Sign up to request clarification or add additional context in comments.

7 Comments

The relevant part of the documentation is here in the "Note" box is says "Fields containing arrays match conditional operators, if only one item matches." That is the crux of the issue, I think.
@mayhewr.. I'm not really sure whether, that is relevant here. I think, here the concern is about exact match. However, if the range is concerned, then yes that part is important to remember. :)
You're absolutely right. I read it too quickly. That bit merely assumes the point I thought it was trying to make. The documentation seems to assume that you understand that equals is equivalent to contains in mongo queries, which I don't think is clear to new users at all. The last line of the first note on the linked page should read "This query selects all the documents where the a field holds a value of 42 or an array containing 42." I'll stop rambling now.
@RohitJain This line almost works: db.user.find({codes: {$in: [2, 8]}}, {codes:1}) It's retrieves the correct array and _id but i need to retrieve all fields (_id, name, username, codes and available) from my document. Can i get all fields on result or only array and _id? Thanks in advance. David
@DavidL... Yeah sure. Just remove that 2nd part from there. {codes:1} is used to add that you need only codes field out. Just fire: - db.user.find({codes: {$in: [2, 8]}}), and you are done.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.