【问题标题】:How to remove multiple records in nodejs using mongoose如何使用猫鼬删除nodejs中的多条记录
【发布时间】: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.log tableMDModal.find 获取相同的记录。
  • @AnkushVerma:我不知道..哪种语法是正确的
  • 如果 Records_pids 已经是一个数组,那么去掉 []

标签: node.js mongodb mongoose angular8


【解决方案1】:
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 });
        }

    })
}

错误是语义错误,而不是搜索值 $in: [CS1, CS2, CS3],搜索是 [[CS1, CS2, CS3]] 另外,请查看https://mongoosejs.com/docs/models.html 以定义模型。

MongoDB Enterprise Cluster0-shard-0:PRIMARY> use new
switched to db new
MongoDB Enterprise Cluster0-shard-0:PRIMARY> use neo
switched to db neo
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.coll.insertMany([{  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" }])
{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("5ecb90001a40be1d77da2aa8"),
        ObjectId("5ecb90001a40be1d77da2aa9"),
        ObjectId("5ecb90001a40be1d77da2aaa"),
        ObjectId("5ecb90001a40be1d77da2aab"),
        ObjectId("5ecb90001a40be1d77da2aac")
    ]
}
MongoDB Enterprise Cluster0-shard-0:PRIMARY> const records_id =["Cs1","Cs2","Cs3"]
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.coll.deleteMany({p_id:{$in:records_id}})
{ "acknowledged" : true, "deletedCount" : 3 }

【讨论】:

  • 您介意指定它何时不工作吗? @Vimala K
  • 我只能删除一条记录
  • 我无法删除多条记录
  • 它在语义上是正确的,也许打印tableMDModal.find({ p_id: { $in: records_pids } },看看你得到了什么
  • 如果我尝试删除多条记录,我会收到此错误:CastError: Cast to string failed for value "{ records_pids: 'Cs1,Cs2,Cs3' }" at path "p_id" for model
猜你喜欢
  • 2021-05-23
  • 1970-01-01
  • 2021-06-13
  • 2016-05-04
  • 2021-07-08
  • 2016-06-22
  • 1970-01-01
  • 2017-05-20
  • 2023-01-07
相关资源
最近更新 更多