0

I have documents that look like this in a collection called movies:

{
  "_id" : ObjectId("51c272623021490007000001"),
  "movies": [
    {
      "name": "Booty Call"
      "play_times": []
    },
    {
      "name": "Bulletproof"
      "play_times": [{...},{...}]
    }
  ]
}

I would like to query for documents where "play_times" is not empty or null. Basically I only care about movies with play times.

1 Answer 1

1

If you want to query for separate array elements within documents, that is not possible AFAIK. If you want to get documents which have non-empty play_times, then use $size operator:

movies.play_times : { $size : { $gt : 0} }

To check if field exists, there is an $exists operator.

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

3 Comments

Will this return documents who have one or more non-empty play_times or documents whose movies ALL have non-empty play_times?
Try it and find yourself :) AFAIK, it will return those which have at least one non-empty movie. MongoDB is not very good at querying of nested arrays unfortunately... You may want to redesign your database to flatten it out (MongoDB is good at flat redundant records which don't need joins, because they already look like joined tables - try it).
It didn't work so I was wondering what you expected. Of course I'm going to try it myself

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.