0
/* 1 */
{
    "_id" : ObjectId("5b17aa58240d110001387ddd"),
    "user_id" : ObjectId("5896baf66b95266c0686a917"),
    "name" : "My Collection",
    "lower_case_name" : "my collection",
    "problem_ids" : [ 
        ObjectId("5b17a952240d110001f2a0fc")
    ],
    "created_at" : ISODate("2018-06-06T09:33:12.734Z"),
    "modified_at" : ISODate("2018-06-11T11:09:47.805Z")
}

/* 2 */
{
    "_id" : ObjectId("5b1e4f9b240d110001444432"),
    "user_id" : ObjectId("5896baf66b95266c0686a917"),
    "name" : "Halla",
    "lower_case_name" : "halla",
    "problem_ids" : [ 
        ObjectId("5b17a952240d110001f2a0fc")
    ],
    "created_at" : ISODate("2018-06-11T10:31:55.924Z"),
    "modified_at" : ISODate("2018-06-11T11:09:45.406Z")
}

I want to remove problem_ids ObjectId("5b17a952240d110001f2a0fc") in a single query currently I am doing

db.collections.update({"user_id": ObjectId("5896baf66b95266c0686a917")}, {$pull: {problem_ids: ObjectId("5b17a952240d110001f2a0fc")}})

But it only removes from the first document

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
ARIF MAHMUD RANA
  • 5,026
  • 3
  • 31
  • 58
  • 1
    _"But it only removes from the first document"_ - that's because update() only updates one document [by default, specify `multi: true`](https://docs.mongodb.com/manual/reference/method/db.collection.update/#multi-parameter)... – CodeCaster Jun 11 '18 at 11:19
  • @CodeCaster thanks – ARIF MAHMUD RANA Jun 11 '18 at 11:25
  • db. collections.update({"user_id": ObjectId("5896baf66b95266c0686a917")}, {$unset: {problem_ids:1}} , { multi: true }); It should work Rana vai :) – Rahul Baruri Jun 11 '18 at 12:06

1 Answers1

0

use {multi:true} to update multiple documents

 db.collections.update(
    {"user_id": ObjectId("5896baf66b95266c0686a917")}, 
    {$pull: {problem_ids: ObjectId("5b17a952240d110001f2a0fc")}},
    {multi: true}
  );
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71