简体   繁体   English

如何检查特定字段是否具有两个或多个值? 派蒙戈,MongoDB

[英]How can I check whether a specific field has two or more values? Pymongo, MongoDB

I have a question.我有个问题。 Some background context: I am querying a database for a specific field whose value is of the type [User].一些背景上下文:我正在查询数据库以查找其值为 [User] 类型的特定字段。 Where "User" is of type ObjectId(x) for some x.对于某些 x,“用户”的类型为 ObjectId(x)。 For example:例如:

{  '_id': ObjectId(x)
   'field_1: value
   'field_2: [User_1]
}

However, field_2 can also have multiple Users within the brackets in other documents like so:但是, field_2 也可以在其他文档的括号内有多个用户,如下所示:

   'field_2': [User_1, User_2, User_3]

So my question is: If I need to narrow down the population, how can I search for the documents that have two or more "User" values in 'field_2'?所以我的问题是:如果我需要缩小人口范围,我如何搜索在“field_2”中有两个或多个“用户”值的文档?

You can use mongo query operators in order to narrow down your results.您可以使用mongo 查询运算符来缩小结果范围。

For your purposes, a combination of $and , $size , and $not should work.出于您的目的, $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.

相关问题 Pymongo - 如何返回特定字段? - Pymongo - How can I return a specific field? 如何检查网站是否装有JavaScript? - How can I check whether a website has javascript or not? 如何使用python和pymongo制作二维结构(词典中的词典)以存储在MongoDB中? - How can I make a two dimensional structure(dictionary inside a dictionary) using python and pymongo to store in MongoDB? 在Django Python中添加新卡之前,我如何检查Stripe客户是否已拥有特定卡 - how can i check whether a Stripe customer already has a specific card before adding a new one in django python Pymongo:如何检查字段是否存在 - Pymongo: How to check if field exists pymongo-如何为字段以及其他查询参数设置不同的值 - pymongo- How can I have distinct values for a field along with other query parameters 如何在PyMongo中执行等效的SQL Join? 或更具体地,用BSON代码调用Pymongo集合对象? - How do I perform the SQL Join equivalent in PyMongo? Or more specific call a Pymongo collection object in BSON code? pymongo:如何从mongodb中提交多个结果? - pymongo : How can I get multiple results from filed in mongodb? 如何将 Somethink 添加到 MongoDB 列表对象,[PyMongo] - How Can I Add Somethink To MongoDB list object , [ PyMongo ] 如何通过 pymongo 验证用于 mongodb 身份验证的用户名密码? - how can i validate username password for mongodb authentication through pymongo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM