Javascript 更新或向上插入多个文档mongodb
我正在使用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
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是唯一的选项,不确定是否还有其他选项。。