【问题标题】:Delete Array item from mongoDB collection从 mongoDB 集合中删除数组项
【发布时间】:2016-03-16 14:34:11
【问题描述】:

我正在努力追随,如果有人可以提供帮助将是很大的帮助

我想从 items 数组中删除一个项目

 {
"_id" : ObjectId("56e8c56a71cfa3fbe528c91b"),
"shipping" : 6.7800000000000002,
"subTotal" : 201,
"totalCost" : 207.7800000000000011,
"order_status" : "queued",
"ordered_by" : "John Sims",
"ordered_by_id" : "56e8c50a71cfa3fbe5288a48",
"created_at" : ISODate("2016-03-16T02:31:06.723Z"),
"items" : [ 

    {
        "created_at" : "2008-12-17T11:01:23.460Z",
        "quantity" : 1,
        "country" : "Australia",
        "state" : "NSW",
        "suburb" : "Sydney",
        "location" : "Sydney,NSW,Australia",
        "longitude" : "151.1228434",
        "latitude" : "-33.7904955",
        "genre" : "Rock",
        "cost" : "67",
        "product_status" : "pending",
        "product_by" : "Paul Dirac",
        "item" : "CD",
        "cart_id" : "56e8c56271cfa3fbe528c918"
    }, 

    {
        "created_at" : "2008-12-17T11:01:23.460Z",
        "quantity" : 1,
        "country" : "Australia",
        "state" : "QLD",
        "suburb" : "Brisbane",
        "location" : "Brisbane,NSW,Australia",
        "longitude" : "151.1228434",
        "latitude" : "-33.7904955",
        "genre" : "",
        "cost" : "67",
        "product_status" : "pending",
        "product_by" : "Paul Dirac",
        "item" : "DVD",
        "cart_id" : "56e8c56571cfa3fbe528c919"
    }
 ]
} 

基于以下条件

// Updates an existing order in the DB.
exports.updateArray = function(req, res) {
Order.findById(req.params.id, function (err, order) {
  if (err) { return handleError(res, err); }
  if(!order) { return res.send(404); }
   order.update(
   // query 
   {

     },

   // update 
  {
    $pull:{
      items:{
        "item" : "CD"
        }
     }
    }
  );
  order.save(function (err) {
    if (err) { return handleError(res, err); }
    return res.json(200, order);
   });
 });
};

现在更新代码运行并在从 shell 执行代码时从项目中删除项目

db.getCollection('orders').update(
// query 
{

},

// update 
{$pull:{
    items:{
        item:'CD'
        }
    }
},

// options 
{
    "multi" : false,  // update only one document 
    "upsert" : false  // insert a new document, if no existing       document match the query 
}
 );

但是当运行 put 或 delete 请求时,这不会从特定文档的 items 数组中删除该项目。如果有人可以帮助我弄清楚代码需要更改以使其通过 HTTP 请求发生,那就太好了。

谢谢

【问题讨论】:

  • 您的问题是您在不需要时调用(试图调用).findById().save() 方法。 .update() 方法完成了“查找”和“保存/更新”数据的所有工作。这是一个在服务器上发生的原子操作。这就是该方法的用途。
  • 感谢@BlakesSeven 成功了

标签: arrays node.js mongodb express


【解决方案1】:

db.collection.findOneAndUpdate({_id:'56e8c56a71cfa3fbe528c91b'}, {$pull:{"items.item":"cd"}},{new:true},function(err,object{ }); 您可以在 mongodb 查询中使用 $pull 。 我在 ndoe js 中的猫鼬中使用了这个查询,它可能对我有用..

【讨论】:

    【解决方案2】:

    工作解决方案现在是这个

    exports.updateArray = function(req, res) {
    Order.update(
     // query 
      {
       _id:req.params.id
     },
    
    // update 
     {
       $pull:{
         items:{
            item:itemdata
            }
        }
      },function(err){
       return res.send(204);
      }
     );
     };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-08
      • 2020-06-30
      • 2015-02-28
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多