Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove embedded document in a nested array of documents

Tags:

mongodb

My schema looks like this:

"content" : [
        {
            "_id" : ObjectId("4fc63de85b20fb72290000f8"),
            "assets" : [
                {
                    "path" : "temp/4f840af9565832fa14000002/4f840b1e565832fa14000007/4fc63de85b20fb72290000f7/content/I_Understanding_and_Measuring.pdf",
                    "_id" : ObjectId("4fc63def5b20fb722900010e")
                },
                {
                    "path" : "temp/4f840af9565832fa14000002/4f840b1e565832fa14000007/4fc63de85b20fb72290000f7/content/me.jpg",
                    "_id" : ObjectId("4fc63e4d5b20fb722900015d")
                }
            ],
            "content" : "",
            "name" : "Downloads"
        },
        {
            "_id" : ObjectId("4fc63dfd5b20fb722900012a"),
            "assets" : [
                {
                    "path" : "temp/4f840af9565832fa14000002/4f840b1e565832fa14000007/4fc63de85b20fb72290000f7/content/me.jpg",
                    "_id" : ObjectId("4fc63e055b20fb7229000147")
                },
                {
                    "path" : "temp/4f840af9565832fa14000002/4f840b1e565832fa14000007/4fc63de85b20fb72290000f7/content/thierry-henry-12-31-11-1.jpg",
                    "_id" : ObjectId("4fc63e525b20fb7229000164")
                }
            ],
            "content" : "",
            "name" : "Bio"
        }
    ],

I can retrieve this document with:

db.presentations.find({'content.assets._id': ObjectId('4fc63def5b20fb722900010e')})`

I've tried the following to remove an document from the assets collection with the line below, but to no avail:

db.presentations.update(
  {'content.assets._id': ObjectId('4fc63def5b20fb722900010e')}, 
  {$pull: {'content.assets': {'_id': ObjectId('4fc63def5b20fb722900010e')}}}
)

I'm trying to remove an item from the corresponding assets collection by its id. Any ideas?

like image 978
Mark Nguyen Avatar asked Jun 08 '12 14:06

Mark Nguyen


People also ask

How do I delete a nested document in MongoDB?

To remove an element from a doubly-nested array in MongoDB document, you can use $pull operator. Now field "UserZipCode": "20010" has been removed from a doubly-nested array.

How do you delete an element from an array in MongoDB?

To remove an element, update, and use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.

What is embedded array?

An embedded array is an ASIC under a new method featuring consolidation of “Sea of gates” of a gate array and hard-macro cells for specific applications.

What is sub document in MongoDB?

In Mongoose, subdocuments are documents that are nested in other documents. You can spot a subdocument when a schema is nested in another schema. Note: MongoDB calls subdocuments embedded documents.


1 Answers

You are so close! Remember that your outermost "content" is an array itself. So the following 2 character change works, use content.$.assets inside the value for $pull.

db.presentations.update(
  {'content.assets._id': ObjectId('4fc63def5b20fb722900010e')}, 
  {$pull: {'content.$.assets': {'_id': ObjectId('4fc63def5b20fb722900010e')}}}
)

Zoom ahead.

like image 95
Gary Murakami Avatar answered Oct 18 '22 16:10

Gary Murakami