简体   繁体   English

mongodb查找带有数据字段的文档不存在

[英]mongodb find documents with fields with a data doesn't exist

I have mongodb document like 我有喜欢的mongodb文件

    {
    "_id" : ObjectId("543d563bde1e58511c264340"),
    ...some fields ...
    "pref" : [ 
        {
            "user_id" : 1,
            "value" : 0.56
        }
    ]
}

How can I find all the documents where pref does not contain an entry with user_id :1 ? 如何查找pref不包含user_id :1条目的所有文档?

It's a little unclear what you're looking for here. 您在这里寻找什么还不清楚。 If you want to find all entries where user_id has any other value than '1', then you'd want: 如果要查找user_id具有除'1'以外的任何其他值的所有条目,则需要:

db.collection.find({"pref.user_id": {'$ne': 1}})

If you're looking for documents where the 'user_id' field doesn't exist at all: 如果要查找根本没有'user_id'字段的文档:

db.collection.find({"pref.user_id": {'$exists': 0}})

Keep in mind, though the behavior of both of these queries on a nested array. 请记住,尽管这两个查询在嵌套数组上的行为。 What you're actually going to get is all the documents where any of the objects in the 'pref' array matches the specified condition. 实际上,您将获得所有文档,其中'pref'数组中的任何对象都符合指定条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM