how to make a mongodb collection find (db.collection.find) with empty values?
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)
how do i get it? i already searched ... but didnt found something
thanks!
( already testet mongoDB : multi value field search ignoring null fields)
Versions:
Node: 8.9.0
(npm) mongodb: 2.2.33
mongodb: 3.4
swapnil2993 :
Try using $and, $or operators. Something like.\n\ncol.find({\n$and:[\n {\"date\": {$gte: new Date(content.date.from),$lte: new Date(content.date.to)}},\n {\"$or\":[{\"name\": {$ne: {$type: null}}},{\"name\":test(content.name)}]},\n {\"$or\":[{\"text\": {$ne: {$type: null}}},{\"text\":test(content.text)}]}\n ]\n}).toArray((err, items) => {\n console.log(items)\n});\n",
2017-11-15T10:24:41