mongodb $总是返回0

Ami*_*ash 6 mongodb pymongo

我有一个数据库集合(命名为fols),如下所示:

{'followers':
        {
           '123':1
           '123':2
           '123':3
         }
}
Run Code Online (Sandbox Code Playgroud)

如果我运行查询(使用pymongo):

cursor = fols.find()
cursor.count()
>>3
Run Code Online (Sandbox Code Playgroud)

工作良好.现在:

cursor = fols.find({'followers':{'123':1}})
cursor.count()
>>1
Run Code Online (Sandbox Code Playgroud)

再次正常工作.但如果我尝试:

cursor = fols.find({'followers':{'123':{'$exists': True}}})
cursor.count()
>> 0
Run Code Online (Sandbox Code Playgroud)

即使有3条记录,它也会返回0.

Joh*_*yHK 24

如果未匹配完整对象,则需要使用点表示法对嵌入对象使用运算符.所以在这种情况下:

cursor = fols.find({'followers.123':{'$exists': True}})
Run Code Online (Sandbox Code Playgroud)


Tho*_*mas 5

尝试点语法:

cursor = fols.find({'followers.123': {'$exists': True}})
Run Code Online (Sandbox Code Playgroud)

但也请看我上面的评论.您不能在(子)文档中多次使用相同的密钥.