如何找到空使用mongodb?

时间:2018-07-10 06:35:46

标签: mongodb

我想通过以下方式从数据库中查找

db.xx.find({"fields.name.sourceTexts":null})

db.xx.find({"fields.name.sourceTexts":""})

但无法正常工作并找到全部

[
    {
        "_id": "5b432195e28b99127c59161e",
        "fields": {
            "img": {
                "sourceTexts": [],
                "_class": "us.codecraft.webmagic.selector.PlainText"
            },
            "name": {
                "sourceTexts": [],
                "_class": "us.codecraft.webmagic.selector.PlainText"
            },
            "old": {
                "sourceTexts": [],
                "_class": "us.codecraft.webmagic.selector.PlainText"
            },
            "post": {
                "sourceTexts": [],
                "_class": "us.codecraft.webmagic.selector.PlainText"
            },
            "focusTieba": [],
            "visitor": {
                "sum": 0,
                "list": []
            },
            "follow": {
                "sum": 0,
                "list": []
            },
            "fans": {
                "sum": 0,
                "list": []
            }
        },
        "request": {
            "url": "http://tieba.baidu.com/home/main?un=%DB%A2%D4%B495",
            "cookies": {},
            "headers": {},
            "priority": 0,
            "binaryContent": false
        },
        "skip": false,
        "_class": "us.codecraft.webmagic.ResultItems"
    }
]

2 个答案:

答案 0 :(得分:0)

如果我有需要的东西,我认为您需要下一个查询

db.xx.find({"fields.name.sourceTexts":[]})

答案 1 :(得分:0)

您可以使用$ exists运算符检查字段是否存在,并使用$ size运算符检查数组是否为空。

db.xx.find( { "fields.name.sourceTexts" : { $exists:true, $size:0 } } )