Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb delete subdocument

I'm trying to delete a subdocument in mongoDb but can't succeed in it. I've tried with $update,$push and $pull but I'm not successful.

I have the following document:

db.users.findOne({"_id": ObjectId("545677a9e4b0e0ef9379993c")})
{
    "_class" : "com.xxx.xxx.server.da.User",
    "_id" : ObjectId("545677a9e4b0e0ef9379993c"),
    "bookSummaries" : [
        {
            "_id" : ObjectId("545677e7e4b0e0ef9379993d"),
            "isbn" : "2746969644"
            "title": "test title"
        },      
        {
            "_id" : ObjectId("546a522a44ae4f5e652fdca7"),           
            "loanSummaries" : [
                {
                    "loanStatus" : "REQUESTED",
                    "loanId" : "5473992044aec466a619290c"
                },
                {
                    "loanStatus" : "REQUESTED",
                    "loanId" : "5473997d44aec466a619290d"
                },
                {
                    "loanStatus" : "REQUESTED",
                    "loanId" : "547605a744ae0f0d558ae757"
                }
            ],
            "testArray" : [
                {
                    "title" : "Back to the future",
                    "id" : "test"
                },
                {
                    "title" : "Back to the future",
                    "id" : "test2"
                },
                {
                    "title" : "Back to the future",
                    "id" : "test3"
                },
                "test ",
                "test ",
                "test 2",
                "test 2",
                {
                    "loanStatus" : "REQUESTED"
                }
            ],
            "title" : "Back to the future"
        }
    ]
}

and I'm trying to create the queries to:

  1. delete the whole "testArray" subdocument for only this specific subdocument
  2. delete a specific loanSummaries give its loanId for this specific subdocument

Can you help me creating those queries? I've seen several post like this one, but it is not the same problem.

Thanks a lot

like image 450
chouk Avatar asked Apr 13 '26 08:04

chouk


2 Answers

I recently had the same problem where I had to remove a nested subarray. In the query you need to identify the exact object that you want to remove, and then in your update query you can use the $ sign as index for the object in the array that you want to pull.

var query = {
       "_id" : ObjectId(someUserId),
       "bookSummaries._id" : ObjectId(bookId), //might also work with isbn as long as it is uniq
       "bookSummaries.loanSummaries.loanId" : loanId
 };

var updateQuery = {
      "$pull" : {
          "bookSummaries.$.loanSummaries" : {
                 "loanId": loadId
          }
      }
}

Update

I tried the following queries in my shell which worked fine. You will have to enter the appropirate ids.

var query = { "_id" : ObjectId("53b73b1108fe927c0e00007f"),  "bookSummaries._id" : ObjectId("53b73b1108fe927c0e00007f"), "bookSummaries.loanSummaries.loanId" : "53b73b1108fe927c0e00007f" }

var updateQuery = { "$pull" : { "bookSummaries.$.loanSummaries" : {  "loanId": "53b73b1108fe927c0e00007f"  } } }

Update 2

If you already know the index of item/object you want to remove you can use the queries below in order to achieve this.

Find the document you want to edit

var query = {
   "_id" : ObjectId(someUserId),
};

Unset the object you want to remove

var update1 = {
  "$unset" : {
      "bookSummaries.1.loanSummaries.2" : 1
  }
}

Pull the object which has the value null.

var update2 = {
  "$pull" : {
      "bookSummaries.1.loanSummaries" : null
  }
}

And to remove you can basically use the same queries.

Find the document you want to edit

var query = {
   "_id" : ObjectId(someUserId),
};

Unset the object you want to remove

var update1 = {
  "$unset" : {
      "bookSummaries.0.testArray" : 1
  }
} 
like image 54
cbass Avatar answered Apr 16 '26 07:04

cbass


I had to do something similar but delete multiple subdocuments instead of just one. My query included an array of ids and this worked for me:

User.updateOne(
      { _id: userId },
      {
        $pull: { bookSummaries: { _id: { $in: ids } } },
      });
like image 21
Elsa Avatar answered Apr 16 '26 08:04

Elsa