[英]How can I check whether a specific field has two or more values? Pymongo, MongoDB
我有个问题。 一些背景上下文:我正在查询数据库以查找其值为 [User] 类型的特定字段。 对于某些 x,“用户”的类型为 ObjectId(x)。 例如:
{ '_id': ObjectId(x)
'field_1: value
'field_2: [User_1]
}
但是, field_2 也可以在其他文档的括号内有多个用户,如下所示:
'field_2': [User_1, User_2, User_3]
所以我的问题是:如果我需要缩小人口范围,我如何搜索在“field_2”中有两个或多个“用户”值的文档?
您可以使用mongo 查询运算符来缩小结果范围。
出于您的目的, $and
、 $size
和$not
应该可以工作。
query = { '$and': [
{ 'field_2': { '$exists': 'true'} },
{ 'field_2': { '$not': { '$size': 0 } } },
{ 'field_2': { '$not': { '$size': 1 } } }
] } )
collection.find(query)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.