【发布时间】: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