Mongodb $ always returns 0

I have a database collection (called fols), for example:

{'followers':
        {
           '123':1
           '123':2
           '123':3
         }
}

If I run the query (using pymongo):

cursor = fols.find()
cursor.count()
>>3

It works great. Now:

cursor = fols.find({'followers':{'123':1}})
cursor.count()
>>1

Again works fine. BUT if I try:

cursor = fols.find({'followers':{'123':{'$exists': True}}})
cursor.count()
>> 0

It returns 0, although there are 3 records.

+5
source share
2 answers

If you do not match the complete object, you need to use dot notation to use the operator against the inline object. So in this case:

cursor = fols.find({'followers.123':{'$exists': True}})
+19
source

Try the point syntax:

cursor = fols.find({'followers.123': {'$exists': True}})

But also see my comment above. You cannot have the same key more than once in a (sub) document.

+5
source

All Articles