Deshwal
Deshwal

Reputation: 4162

"Pymongo" query for the field when array length greater than 0

For a collection nameed 'duplicate', we can execute db.duplicate.find( { $where: "this.all_dups.length > 0" } ); but what would be the Python version of this query? I tried using

db.duplicate.find( { "$where": {"$gt":{"all_dups.length"} > 0] }} )

but it is showing me error for obvious reasons.

I also tried

db.duplicate.find( { "all_dups" : { "$size": { "$gt" : 0 }}})

I can do it in Python like:

for a in db.duplicate.find():
    if 'all_dups' in a and len(a['all_dups'])>0:
        print(a)

How can I achieve in PyMongo? I think there $exists, $where, $gt and $size will be used but I don't exactly know how.

Upvotes: 0

Views: 347

Answers (1)

Belly Buster
Belly Buster

Reputation: 8844

You could try a search that excludes an empty list or a blank list:

db.duplicate.find({"all_dups": {'$nin': [[], None]}})

Upvotes: 1

Related Questions