0

I have an array of the id which I get from post request in nodejs. I want to remove all the elements in the orders array which match receive id array.

eg remove_id = ['60f1ab20891ced4818b5ea88','60f1ab20891ced4818b5ea87'] so I want to remove all elements from orders array whoose id match match remove_id array.

orderdetail = {
  _id: 60f1ab20891ced4818b5ea86,
  totalPrice: 400,
  orders: [
    {
      _id: 60f1ab20891ced4818b5ea87,
      quantity: 1,
      price: 200,
      name: 'Caramel Latte',
      category: 'Steaming Cups'
    },
    {
      _id: 60f1ab20891ced4818b5ea88,
      quantity: 1,
      price: 200,
      name: 'Cinnamon Latte',
      category: 'Steaming Cups'
    },
    {
      _id: 60f1ab20891ced4818b5ea89,
      quantity: 1,
      price: 200,
      name: 'Cinnamon Latte',
      category: 'Steaming Cups'
    }
  ],
  timestamp: 1626450720332,
  name: 'xxx',
  email: 'xxx',
}

what I have tried but not working,

Orderdetail.updateOne( { _id: '60f1ab20891ced4818b5ea86' }, { 
        $pull: { order: { _id: { $in: remove_id  } } } } )

1 Answer 1

4

Rename object name order to orders !

Try this code !

let remove_id = ['60f1ab20891ced4818b5ea87','60f1ab20891ced4818b5ea88']
db.getCollection('Orderdetail').updateOne({ _id: '60f1ab20891ced4818b5ea86' }, {
  $pull: {
    orders: { // rename this to orders 
      _id: { $in: remove_id }
    }
  }
})
Sign up to request clarification or add additional context in comments.

1 Comment

sorry for such a smaller issue.

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.