Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在更新期间忽略集合中具有null的元素_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 在更新期间忽略集合中具有null的元素

Node.js 在更新期间忽略集合中具有null的元素,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我试图通过将ObjectId作为请求体值传递来更新模型中的值。有时它在更新,有时不更新。我发现这是因为在某些记录中,ObjectId为空。当集合中没有空值时,其更新将正常。因此,我决定省略记录中具有null值的记录,并查询其余的记录。但我面临一个问题。我无法查询从空省略记录手动存储的集合 代码 accept = async (req, res) => { let ObjectId = require('mongoose').Types.ObjectId try {

我试图通过将
ObjectId
作为请求体值传递来更新模型中的值。有时它在更新,有时不更新。我发现这是因为在某些记录中,
ObjectId
为空。当集合中没有空值时,其更新将正常。因此,我决定省略记录中具有
null
值的记录,并查询其余的记录。但我面临一个问题。我无法查询从空省略记录手动存储的集合

代码

accept = async (req, res) => {
    let ObjectId = require('mongoose').Types.ObjectId
    try {
        await this._model.find({
            person1: {
                $ne: null
            }
        }, (err, obj) => {
            if (err) {
                console.log(err);
            } else {
                console.log(obj);
                obj.update({
                    person1: new ObjectId(req.body.cid),
                    person2: new ObjectId(req.body.uid)
                }, {
                    $set: {
                        status: "active"
                    },
                }, (err, doc) => {

                    if (err) {
                        this.getErrorMsg(res, 522, null);

                    } else {
                        this.getErrorMsg(res, 227, doc);
                    }
                })

            }

        })
    } catch (err) {

        console.log(err);
    }
}
收藏

[{
   "_id":"5d2715cea9ae086ac295c6d8",
   "status":"pending",
   "person1":ObjectId("5cfe4dd898fd225ef1c99ded"),
   "person2":ObjectId("5d02197e2859122fa28c189b")
}
{
   "_id":"5d2715cea9ae086ac295c6d9",
   "status":"pending",
   "person1":ObjectId("5cfe4dd898fd225ef1c99dee"),
   "person2":ObjectId("5d02197e2859122fa28c189c")
}
{
   "_id":"5d2715cea9ae086ac295c6d0",
   "status":"pending",
   "person1":null,
   "person2":ObjectId("5d02197e2859122fa28c189f")
}]
错误

TypeError:obj.update不是一个函数


您只需要将
person1:{$ne:null}
添加到更新查询的过滤器中

例如:

await this._model.update({
  person1: {$ne: null, $eq: new ObjectId(req.body.cid)},
  person2: new ObjectId(req.body.uid)
}, {
  $set: {
    status: "active"
  }
})

您只需要将
person1:{$ne:null}
添加到更新查询的过滤器中

例如:

await this._model.update({
  person1: {$ne: null, $eq: new ObjectId(req.body.cid)},
  person2: new ObjectId(req.body.uid)
}, {
  $set: {
    status: "active"
  }
})