0

i have a trouble. i need to update value in nected array (array in array). For example i have document like this:

{
    "_id" : ObjectId("59eccf5ea7f6ff30be74d8ce"),
    "name" : "some name",
    "description" : "some description",
    "users" : [ 
    {
        "id" : ObjectId("59d1549f4f5c6f6e0f1d6576"),
        "technologies" : [
            {"id": ObjectId("59450bc718fda360fdf4a719")},       
        ]
    }, 
    {
        "id": ObjectId("59d1549e4f5c6f6e0f1d6571"),
        "technologies": [
            {"id": ObjectId("59450f8318fda360fdf4a78b")},
            {"id": ObjectId("59450bc718fda360fdf4a719")},
            {"id": ObjectId("59450e3f18fda360fdf4a767")}
        ]
    },
    {
        "id": ObjectId("59d154a44f5c6f6e0f1d65af"),
        "technologies": [
            ObjectId("59450f8318fda360fdf4a78b")
        ]
    }
    ]
}

i need to delete exact technology from exact user. i know only:

_id - global document id

userId: 'users.id' element

technologyId: 'users.$.technologies.$.id' id of technology item that should be deleted

documentation of mongo says that i cant use two $ in update statement, but maybe is exists some actions to awoid this?

1
  • I have answered below, but I'm writing to mention that you probably have a typo in the third user technologies array. There is no id field. Commented Oct 25, 2017 at 22:21

1 Answer 1

2

Try the following:

db.yourColl.update(
    {
        "_id": ObjectId("59eccf5ea7f6ff30be74d8ce"),
        "users.id": ObjectId("59d1549e4f5c6f6e0f1d6571")
    },
    {
        "$pull": {
            "users.$.technologies": {
                "id": ObjectId("59450bc718fda360fdf4a719")
            }
        }
    }
)

The result should be:

{ 
    "_id" : ObjectId("59eccf5ea7f6ff30be74d8ce"), 
    "name" : "some name", 
    "description" : "some description", 
    "users" : [
        {
            "id" : ObjectId("59d1549f4f5c6f6e0f1d6576"), 
            "technologies" : [
                {
                    "id" : ObjectId("59450bc718fda360fdf4a719")
                }
            ]
        }, 
        {
            "id" : ObjectId("59d1549e4f5c6f6e0f1d6571"), 
            "technologies" : [
                {
                    "id" : ObjectId("59450f8318fda360fdf4a78b")
                }, 
                {
                    "id" : ObjectId("59450e3f18fda360fdf4a767")
                }
            ]
        }, 
        {
            "id" : ObjectId("59d154a44f5c6f6e0f1d65af"), 
            "technologies" : [
                ObjectId("59450f8318fda360fdf4a78b")
            ]
        }
    ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

You are totally right. I had this solution, but before use it i broke my structure and it did not work. You gave me new way to look at problem, thanks.

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.