【发布时间】:2020-09-11 07:54:54
【问题描述】:
尝试使用 mongoose 从 mongodb 的单个调用中删除多个记录,但无法正常工作。我想更改代码的位置。请帮助找到解决方案。
在我的代码中,如果我这样使用..它正在工作..
({ p_id: { $in: ['Cs1', 'Cs2', 'Cs3']} }
但是如果像下面这样使用
({ p_id: { $in: [records_pids] } } 它不起作用。因为我通过 api 调用获取此数组值。
MongoDB:
{
p_id:"Cs1",
name:"Test",
value:"Power"
},
{
p_id:"Cs2",
name:"Test",
value:"Power"
},
{
p_id:"Cs3",
name:"Test",
value:"Power"
},
{
p_id:"Cs4",
name:"Test",
value:"Power"
},
{
p_id:"Cs5",
name:"Test",
value:"Power"
}
data.controller.js:
module.exports.deleteMultipleRecord = (req, res, next) => {
var collectionMDName = req.query.collectionname;
var records_pids = req.query.pids; //Array value Cs1, Cs2, Cs3
var tableMDModal = mongoose.model(collectionMDName);
tableMDModal.deleteMany({ p_id: { $in: [records_pids] } }, function(err, docs) {
if (err) {
console.log('ss' + err);
return
} else {
console.log("Successful deleted selected records");
res.json({ data: docs, success: true, msg: 'Successful deleted selected records.', cname: collectionMDName });
}
})
}
【问题讨论】:
-
@taha: 我已经看过那个链接但是没用..我不知道怎么用
-
你看到了什么错误。如果您没有看到任何记录,请
console.logtableMDModal.find获取相同的记录。 -
@AnkushVerma:我不知道..哪种语法是正确的
-
如果 Records_pids 已经是一个数组,那么去掉 []
标签: node.js mongodb mongoose angular8