简体   繁体   English

(mongoDB)查找具有空值或空值的

[英](mongoDB) find with empty or null values

how to make a mongodb collection find (db.collection.find) with empty values? 如何使mongodb集合查找(db.collection.find)为空值?

currently i have: 目前我有:

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)
});

but it returns an empty array, because "name" or "text" is null / an empty string, i want that it query only the values that have something specified or ignore it (like content.name is something in it or its empty) 但是它返回一个空数组,因为“名称”或“文本”为空/一个空字符串,我希望它仅查询指定了值的值或将其忽略(例如content.name是其中的值或为空)

how do i get it? 我怎么得到它? i already searched ... but didnt found something 我已经搜索过...但是没有找到东西

thanks! 谢谢!

( already testet mongoDB : multi value field search ignoring null fields ) (已经testet mongoDB:多值字段搜索,忽略空字段

Versions: Node: 8.9.0 (npm) mongodb: 2.2.33 mongodb: 3.4 版本:节点:8.9.0(npm)mongodb:2.2.33 mongodb:3.4

Try using $and, $or operators. 尝试使用$ and,$ or运算符。 Something like. 就像是。

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)
});
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 }
            ]
        }
    ]
})

Edited: 编辑:

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 }
            ]
        }
    ]
})

null and empty is different, you need to add one more condition for empty string in query. nullempty是不同的,您需要为查询中的空字符串添加一个条件。

Logical Query Operators 逻辑查询运算符

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

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