1

I am trying to do following, if anyone could help would be great help

I want to delete an item from the items array

 {
"_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"
    }
 ]
} 

Based on conditions like below

// 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);
   });
 });
};

Now update code runs and remove item from items when code being executed from shell with following

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 
}
 );

But when run over put or delete request, this doesn't remove the item from the items array of the specific document. If anyone could help me figure out the code needs to change to make it happen over HTTP request would be great.

Thanks

2
  • Your problem is you are calling ( trying to call ) .findById() and .save() methods when you don't have to. The .update() method does all the work of both "finding" and "saving/updating" the data. It's an atomic operation that happens all on the server. That's what the method is for. Commented Mar 16, 2016 at 3:34
  • Thanks @BlakesSeven it worked Commented Mar 16, 2016 at 6:30

2 Answers 2

1

db.collection.findOneAndUpdate({_id:'56e8c56a71cfa3fbe528c91b'}, {$pull:{"items.item":"cd"}},{new:true},function(err,object{ }); You can use $pull in mongodb query . this query i used in mongoose in ndoe js and it could be work for me..

Sign up to request clarification or add additional context in comments.

Comments

0

Working Solution is now this

exports.updateArray = function(req, res) {
Order.update(
 // query 
  {
   _id:req.params.id
 },

// update 
 {
   $pull:{
     items:{
        item:itemdata
        }
    }
  },function(err){
   return res.send(204);
  }
 );
 };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.