1

I have some data structured like this in a mongo collection:

{ "list" : [ { "update_time" : NumberLong(1426690563), "provider" : NumberLong(4) } ] }

What I would like to do is query for all of the entries where the list includes a provider value of 4.

If it helps, all of the list arrays contain only one element.

What I am trying right now is this:

db.collection.find(
    {
      list: {
              provider: 4
            }
    }
)

This does not work though, and always returns an empty set

1
  • This is the solution Commented Mar 18, 2015 at 15:33

1 Answer 1

1

Use the dot notation i.e. concatenate the name of the field that contains the array, with a dot (.) and the name of the field in the embedded document and use that as your query:

db.collection.find({"list.provider": 4})

You can also use the $elemMatch operator to specify multiple criteria on an array of embedded documents such that at least one embedded document satisfies all the specified criteria:

db.collection.find({
    list: {
        $elemMatch: {
            provider: 4
        }
    }
})
Sign up to request clarification or add additional context in comments.

Comments

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.