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
})