【问题标题】:(mongoDB) find with empty or null values(mongoDB) 查找空值或空值
【发布时间】:2017-11-15 09:30:56
【问题描述】:

如何使用空值创建 mongodb 集合查找 (db.collection.find)?

目前我有:

function test(s) {
    if (!s) {
        return null;
    } else {
        return s;
    }
}

var content = {
    date: { 
        from: '2017-10-15', 
        to: '2017-11-15' 
    },

    'name': 'some text', //this can be null or empty
    'text': 'some other text' //this can be null or empty
}

col.find({
    "date": {
        $gte: new Date(content.date.from),
        $lte: new Date(content.date.to)
    },

    "name": {
        $ne: {
            $type: null
        },

        $eq: test(content.name)
    },

    "text": {
        $ne: {
            $type: null
        },

        $eq: test(content.text)
    },
}).toArray((err, items) => {
    console.log(items)
});

但它返回一个空数组,因为“name”或“text”为空/空字符串, 我希望它只查询具有指定内容或忽略它的值(例如 content.name 是其中的内容或为空)

我如何得到它?我已经搜索过...但没有找到任何东西

谢谢!

(已经测试过mongoDB : multi value field search ignoring null fields

版本: 节点:8.9.0 (npm) MongoDB: 2.2.33 MongoDB:3.4

【问题讨论】:

    标签: javascript node.js mongodb


    【解决方案1】:

    尝试使用 $and、$or 运算符。类似的东西。

    col.find({
    $and:[
        {"date": {$gte: new Date(content.date.from),$lte: new Date(content.date.to)}},
        {"$or":[{"name": {$ne: {$type: null}}},{"name":test(content.name)}]},
        {"$or":[{"text": {$ne: {$type: null}}},{"text":test(content.text)}]}
     ]
    }).toArray((err, items) => {
      console.log(items)
    });
    

    【讨论】:

    • 试图使,当“名称”是什么(除了空),它仍然找到所有其他的,就像我有“t1”,“t2”并且在“t1”之后搜索仍然找到“t2 "(姓名)
    【解决方案2】:
    col.find({
        $and: [
            {
                $and: [
                    { 
                        "date": {
                            $gte: new Date(content.date.from)
                        }
                    },
                    { 
                        "date": {
                            $lte: new Date(content.date.to)
                        }
                    },
                ]
            },
            {
                $or: [
                    { 'name': null },
                    { 'name': content.name }
                ]
            },
            {
                $or: [
                    { 'text': null },
                    { 'text': content.text }
                ]
            }
        ]
    })
    

    已编辑:

    col.find({
        $and: [
            {
                $and: [
                    { 
                        "date": {
                            $gte: new Date(content.date.from)
                        }
                    },
                    { 
                        "date": {
                            $lte: new Date(content.date.to)
                        }
                    },
                ]
            },
            {
                $or: [
                    { 'name': null },
                    { 'name':'' }
                    { 'name': content.name }
                ]
            },
            {
                $or: [
                    { 'text': null },
                    { 'text': '' },
                    { 'text': content.text }
                ]
            }
        ]
    })
    

    nullempty不同,需要在查询中为空字符串再添加一个条件。

    Logical Query Operators

    【讨论】:

    • 这里有这个,它总是以空(数组,当.toArray)响应,我已经读过这个,我在操作符列表中没有发现任何有用的......
    • 这个查询对我来说看起来很正确,所以你可能想从不使用任何过滤器开始,看看是否有任何返回,然后逐步添加过滤器,看看哪个导致空数组.
    • 对我来说,这仅在“content.name”和“content.text”具有值时才有效......(当它们中只有一个或任何内容被定义或为空时,它总是以空响应数组)
    • 我需要它,以便输入值为空/空/带有字符串(输入)并且查询搜索它,当它有输入时,当它为空/空时,它应该忽略它(从输入(内容))
    猜你喜欢
    • 1970-01-01
    • 2022-12-22
    • 1970-01-01
    • 2013-02-11
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多