4

I have a document that has a array of ObjectIDs for reference. That array is not an associative array, it's just the ObjectIDS:

{
  ...
  "tags" : [ 
    ObjectId("54744662ae8a0be602568c4f")
  ]
}

Now I'm trying to filter based on that array, like this:

db.expenses.find({
  tags:{
    $elemMatch:{ObjectId("547469bb0bde915a05f74299")}
  }
})

I know that the projection on the $elemMatch is wrong, but I just don't know how to do it, and Google isn't helping must because most of the examples are for and array of json objects with defined properties.

Does anyone know how to do this?

1 Answer 1

6

If I got your problem correctly, this would be the answer

db.expenses.find(
{
  tags: {$in : [ObjectId("54744662ae8a0be602568c4f")]}  
})
Sign up to request clarification or add additional context in comments.

3 Comments

You don't need the $in though since it's just a single value, so it can just be tags: ObjectId("54744662ae8a0be602568c4f")
@JohnnyHK is right, but I used $in for searching multiple ObjectIds if its needed
I feel really dumb right now, your right, I have no clue why I didn't try this. I think the array thing made me think it should be different. Thanks

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.