learner
learner

Reputation: 153

removing an object from a mongodb document

I have a document in Mongodb collection, where I want to remove an object, using title key.

I tried using $unset, but it only removes the title key not the object to which it belongs.

{
    "_id" : ObjectId("576b63d49d20504c1360f688"),
    "books" : [
        {
            "art_id" : ObjectId("574e68e5ac9fbac82489b689"),
            "title" :"abc",
            "price" : 40
        },
        {
            "art_id" : ObjectId("575f9badada0500d192c53f4"),
            "title" : "xyz",
            "price" : 20
        },
        {
            "art_id" : ObjectId("57458224d86b3d1561150f17"),
            "title" : "def",,
            "price" : 30
        }
    ],
    "user_id" : "575570c315e27d13167dfc0d"
}

Upvotes: 1

Views: 24959

Answers (5)

Revol89
Revol89

Reputation: 888

And if... ¿Do I want to delete an object that is inside a document and not as an array?

{
    "_id" : ObjectId("576b63d49d20504c1360f688"),
    "books" : {
        "574e68e5ac9fbac82489b689": {
            "art_id" : ObjectId("574e68e5ac9fbac82489b689"),
            "title" :"abc",
            "price" : 40
        },
        "575f9badada0500d192c53f4": {
            "art_id" : ObjectId("575f9badada0500d192c53f4"),
            "title" :"xyz",
            "price" : 20
        },
        "57458224d86b3d1561150f17": {
            "art_id" : ObjectId("57458224d86b3d1561150f17"),
            "title" : "def",
            "price" : 30
        }
    },
    "user_id" : "575570c315e27d13167dfc0d"
}

The solutions is this:

db.auctions.update(
    {'_id': ObjectId("576b63d49d20504c1360f688")},
    {$unset: {"books.574e68e5ac9fbac82489b689":
        {_id: "574e68e5ac9fbac82489b689"}}})

Upvotes: 1

Nidhin David
Nidhin David

Reputation: 2474

To remove the entire object that contains the query object use db.remove() query.

For your case:

db.yourcollection.remove({"books.title": "abc"});

Please double check the format in which the element of array is referenced. This removes the entire objects that contains the embedded query obj. To remove only a single object, provide it with another field to uniquely identify it.

If you only want to remove the object that contains the title field from the array but wants to keep the object that contains the array, then please use the $pull operator. This answer will be of help.

Example: if you want to remove object

        {
            "art_id" : ObjectId("574e68e5ac9fbac82489b689"),
            "title" :"abc",
            "price" : 40
        }

just from the array but keep the parent object like

{
    "_id" : ObjectId("576b63d49d20504c1360f688"),
    "books" : [
        {
            "art_id" : ObjectId("575f9badada0500d192c53f4"),
            "title" : "xyz",
            "price" : 20
        },
        {
            "art_id" : ObjectId("57458224d86b3d1561150f17"),
            "title" : "def",,
            "price" : 30
        }
    ],
    "user_id" : "575570c315e27d13167dfc0d"
}

use

db.mycollection.update(
    {'_id': ObjectId("576b63d49d20504c1360f688")}, 
    { $pull: { "books" : { "title": "abc" } } },
false,
true 
);

Upvotes: 4

Thishani Lucas
Thishani Lucas

Reputation: 580

Try in the mongo shell

db.yourcollection.remove({books:[{title:'title_you_want'}]})

Careful with the braces.

Upvotes: 0

Shrabanee
Shrabanee

Reputation: 2776

$unset won't remove the object from an array. The $unset operator deletes a particular field. doc.

Use $pull instead.

The $pull operator removes from an existing array all instances of a value or values that match a specified condition.

Try following query

db.collName.update({$pull : {books:{title:abc}}})

Refer $pull-doc

Hope this will help you.

Upvotes: 2

Abdul Rehman Sayed
Abdul Rehman Sayed

Reputation: 6672

Try using pull.

https://docs.mongodb.com/manual/reference/operator/update/pull/

$pull

The $pull operator removes from an existing array all instances of a value or values that match a specified condition.

The $pull operator has the form:

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

To specify a <field> in an embedded document or in an array, use dot notation.

Upvotes: 0

Related Questions