0

I have something like this one.

{
    "title" : "",
    "source" : {
        "object" : "some",
        "group" : "some
        "data" : []
    }
}

"source.data" or "source" can be undefined at all.

So now I need to select all objects that has count(xxx.source.data) > 0 . How can I do this query? I search for count() and xxx.length (method\property) but still can't understand how to use them in my case.

Added:

.find({"source.data": {$exists: true, $size: {$gt: 1}}})
user1954544
  • 1,619
  • 5
  • 26
  • 53

1 Answers1

3

You can do it as follows :

db.myObject.find({$and:[{"source.data":{$exists:true}},{"source.data":{$not:{$size:0}}}]})

From the mongoDB documentation, it says $size operator does not accept range queries.

$size does not accept ranges of values. To select documents based on fields with different numbers of elements, create a counter field that you increment when you add elements to a field.

For greater queries, you can use list index. You can use the following query to find the documents that has data size > 10.

db.myObject.find({$and:[{"source.data":{$exists:true}},{"source.data.10" : {$exists:true}}]})
Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83