Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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
Javascript 更新或向上插入多个文档mongodb_Javascript_Node.js_Mongodb_Mongoose Schema - Fatal编程技术网

Javascript 更新或向上插入多个文档mongodb

Javascript 更新或向上插入多个文档mongodb,javascript,node.js,mongodb,mongoose-schema,Javascript,Node.js,Mongodb,Mongoose Schema,我正在使用mongodb数据库进行一个项目,我想更新或上传多个文档 下面是我想做的: if (you received an array of id ) { create a verify condition wich check if there is a document for each id and respect the dateFin condition } else if (you recieved one id) { create a verify condit

我正在使用mongodb数据库进行一个项目,我想更新或上传多个文档

下面是我想做的:

if (you received an array of id ) 
{
    create a verify condition wich check if there is a document for each id and respect the dateFin condition
}
else if (you recieved one id)
{
    create a verify condition wich check if there is a document equal the id and respect the dateFin condition
}
然后检查是否有文档,然后更新它,或者创建它,或者获得一个数组或一个id

 if (Array.isArray(req.body.user) === true)
                verify = {
                    idUser: { $in: req.body.user.map(id => { return ObjectId(id) }) },
                    dateFin: { $gt: moment().utc(1).format('YYYY-MM-DD HH:MM:SS') }
                }
            else verify = 
                  { 
                    idUser: ObjectId(req.body.user), 
                    dateFin: { $gt: moment().utc(1).format('YYYY-MM-DD HH:MM:SS') } 
                  }
            db.collection("paiement").updateMany(verify,
                    {
                        $set: {
                            dateFin: moment(req.body.date).format('YYYY-MM-DD HH:MM:SS'),
                            paiementType: "offre",
                            abonnement: req.body.abonnement,
                            dateInsert: moment().utc(1).format('YYYY-MM-DD HH:MM:SS'),
                        },
                    },
                    { upsert: true },
                    (err, document) => {
                        if (err) return res.status(400).json(err);
                        console.log(err);
                        console.log(document.result);
                    })
        }

但它只能使用一个id,不能使用多个id,如果有人能帮助我,谢谢你。

这是否回答了你的问题?这一个使用迭代“for”,这需要很多时间,我不希望这样,因为服务器很难运行(假设我使用的是1000 id…)就我所知,对于多个文档的升级,bulkwrite是唯一的选项,不确定是否还有其他选项。。