在Mongoose中查找空字段将不会返回预期结果

时间:2016-11-02 03:05:03

标签: angularjs node.js mongodb express mongoose

我试图在Mongoose中找到一个字段$ ne:null,但它不会返回预期的结果。

代码

{ _id: '5816a7cd404dc92634bbb507',
    prodCode: '1038501090064',
    prodName: 'LISOMUC',
    description: '10 MG / ML XPE CX 50 FR PET AMB X 120 ML + COP (EMB HOSP)',
    PMC18: null }

没有$ ne:null查询的结果

{ _id: '5816a7cd404dc92634bbb507',
    prodCode: '1038501090064',
    prodName: 'LISOMUC',
    description: '10 MG / ML XPE CX 50 FR PET AMB X 120 ML + COP (EMB HOSP)',
    PMC18: null }

结果为$ ne:null查询

_id: String,
prodCode: String,
prodName: String,
description: String,
EAN: String,
PMC18: Number,
manipulationCost: String,
profitRange: String,
inStock: Number,
group: Number

猫鼬产品架构

int multiply (int a, int b) 
{
    int result = 0;
    for (int i = 0; i < a; i++) { //You repeat "a" times...
        for (int j = 0; j < b; j++) { //...adding "b" to result.
            result++;
        }
    }
    return result;
}

我错过了什么?

非常感谢你。

2 个答案:

答案 0 :(得分:0)

Product.find({ PMC18: {$ne: null} }, { prodCode: 1, prodName: 1, description: 1, PMC18: 1, inStock: 1 }, function (err, docs) { console.log(err); 你正确地查询,我认为问题在于引用,因为我没有看到给定代码中的任何错误,因为你得到了意想不到的结果

答案 1 :(得分:0)

请在下面的代码中测试add $ exists:true:

router.get('/service/products/all', function(req, res, next) {
    Product.find({ 'PMC18': {$exists: true, $ne: null} }, { prodCode: 1, prodName: 1, description: 1, PMC18: 1, inStock: 1 }, function (err, docs) {
        console.log(err);        
        console.log(docs);
        res.json(docs);
    }).limit(50);    
});