1

Using native Nodejs driver with MongoDB 2.4 and trying to remove property from documents embedded in array. E.g. how can I remove gym property from all documents embedded in grades array where gym is 562e7c1ae6c3e8ce29abfe05? So from this:

{
    "_id" : ObjectId("562abaaffb5870b8fb9dca3f"),
    "grades" : [
        {
            "_id" : ObjectId("562e7bbe70ee82bb290192bd"),
            "discipline" : "55aae5b7848b9c8bf4ecbb5c",
            "grade" : "55aae5b7848b9c8bf4ecbb4c",
            "gym" : "562e7c1ae6c3e8ce29abfe06",
            "registeredOn" : ISODate("2015-10-26T19:15:10.565Z")
        },
        {
            "discipline" : "55aae5b7848b9c8bf4ecbb5c",
            "grade" : "55aae5b7848b9c8bf4ecbb4d",
            "gym" : "562e7c1ae6c3e8ce29abfe05",
            "registeredOn" : ISODate("2015-10-26T19:17:08.603Z"),
            "_id" : ObjectId("562e7c34e6c3e8ce29abfe06")
        },
        {
            "discipline" : "55aae5b7848b9c8bf4ecbb5c",
            "grade" : "55aae5b7848b9c8bf4ecbb4d",
            "gym" : "562e7c1ae6c3e8ce29abfe05",
            "registeredOn" : ISODate("2015-10-26T19:17:08.603Z"),
            "_id" : ObjectId("562e7c34e6c3e8ce29abfe07")
        }
    ],
},
{
    "_id" : ObjectId("562abaaffb5870b8fb9dca3f"),
    "grades" : [
        {
            "_id" : ObjectId("562e7bbe70ee82bb290192bd"),
            "discipline" : "55aae5b7848b9c8bf4ecbb5c",
            "grade" : "55aae5b7848b9c8bf4ecbb4c",
            "gym" : "562e7c1ae6c3e8ce29abfe07",
            "registeredOn" : ISODate("2015-10-26T19:15:10.565Z")
        },
        {
            "discipline" : "55aae5b7848b9c8bf4ecbb5c",
            "grade" : "55aae5b7848b9c8bf4ecbb4d",
            "gym" : "562e7c1ae6c3e8ce29abfe05",
            "registeredOn" : ISODate("2015-10-26T19:17:08.603Z"),
            "_id" : ObjectId("562e7c34e6c3e8ce29abfe06")
        }
    ],
}

I need this:

{
    "_id" : ObjectId("562abaaffb5870b8fb9dca3f"),
    "grades" : [
        {
            "_id" : ObjectId("562e7bbe70ee82bb290192bd"),
            "discipline" : "55aae5b7848b9c8bf4ecbb5c",
            "grade" : "55aae5b7848b9c8bf4ecbb4c",
            "gym" : "562e7c1ae6c3e8ce29abfe06",
            "registeredOn" : ISODate("2015-10-26T19:15:10.565Z")
        },
        {
            "discipline" : "55aae5b7848b9c8bf4ecbb5c",
            "grade" : "55aae5b7848b9c8bf4ecbb4d",
            "registeredOn" : ISODate("2015-10-26T19:17:08.603Z"),
            "_id" : ObjectId("562e7c34e6c3e8ce29abfe06")
        },
        {
            "discipline" : "55aae5b7848b9c8bf4ecbb5c",
            "grade" : "55aae5b7848b9c8bf4ecbb4d",
            "registeredOn" : ISODate("2015-10-26T19:17:08.603Z"),
            "_id" : ObjectId("562e7c34e6c3e8ce29abfe07")
        }
    ],
},
{
    "_id" : ObjectId("562abaaffb5870b8fb9dca3f"),
    "grades" : [
        {
            "_id" : ObjectId("562e7bbe70ee82bb290192bd"),
            "discipline" : "55aae5b7848b9c8bf4ecbb5c",
            "grade" : "55aae5b7848b9c8bf4ecbb4c",
            "gym" : "562e7c1ae6c3e8ce29abfe07",
            "registeredOn" : ISODate("2015-10-26T19:15:10.565Z")
        },
        {
            "discipline" : "55aae5b7848b9c8bf4ecbb5c",
            "grade" : "55aae5b7848b9c8bf4ecbb4d",
            "registeredOn" : ISODate("2015-10-26T19:17:08.603Z"),
            "_id" : ObjectId("562e7c34e6c3e8ce29abfe06")
        }
    ],
}

The query below will delete only first matching document with id I specified. The second document with "_id" : ObjectId("562e7c34e6c3e8ce29abfe07") will however still have gym property 562e7c1ae6c3e8ce29abfe05. multi seems redundant and so is there any way to remove all gym properties with id I specified?

collections.users.update({
    "grades.gym": "562e7c1ae6c3e8ce29abfe05"
}, {
    $unset: {
        "grades.$.gym": "562e7c1ae6c3e8ce29abfe05"

    }
}, {
    multi: true
})

1 Answer 1

1

Best to use .find() to return the cursor, iterate over it using the .each() method to access each document in the collection that matches the criteria, thereby getting the array field, delete the property which matches the condition and update the document, something like the following:

collection.find({ "grades.gym": "562e7c1ae6c3e8ce29abfe05" }).each(function(err, doc){
    // handle err
    var new_grades = doc.grades.map(function (grade){ 
        if (grade.gym == "562e7c1ae6c3e8ce29abfe05") { delete grade.gym; }
        return grade;
    });

    collection.update({ "_id": doc._id },{ "$set": { "grades": new_grades }});
});
Sign up to request clarification or add additional context in comments.

Comments

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.