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}}})

1 Answer 1

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}}]})
Sign up to request clarification or add additional context in comments.

4 Comments

It doesn't work if "source" or "source.data" not defined. (PS: Edited top message.)
I tried this one. (added to top) So it check existence but not selecting 'size is greater then', where can be trouble?
I have updated query. Try this one. It checks existence & size of the data array.
Yes it works, but can you describe how to set 'greater then' ? For example want to select all where count > 10 ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.