在MongoDB 2.4中使用本机Nodejs驱动程序并尝试从嵌入在数组中的文档中删除属性。例如。如何从gym
grades
gym
中562e7c1ae6c3e8ce29abfe05
数组中嵌入的所有文档中删除{
"_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")
}
],
}
属性?所以从这个:
{
"_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")
}
],
}
我需要这个:
"_id" : ObjectId("562e7c34e6c3e8ce29abfe07")
下面的查询将仅删除id为我指定的第一个匹配文档。但是,562e7c1ae6c3e8ce29abfe05
的第二个文档仍然具有健身房属性multi
。 collections.users.update({
"grades.gym": "562e7c1ae6c3e8ce29abfe05"
}, {
$unset: {
"grades.$.gym": "562e7c1ae6c3e8ce29abfe05"
}
}, {
multi: true
})
似乎是多余的,所以有没有办法删除我指定id的所有健身房属性?
let x:Double = 72.535
var xdp:Int = 0
if let decimalString = String(x).componentsSeparatedByString(".").last, let decimal = Int(decimalString) {
xdp = decimal // 535
}
答案 0 :(得分:1)
最好使用 .find()
来返回光标,使用 .each()
方法对其进行迭代,以访问匹配的集合中的每个文档条件,从而获取数组字段,删除匹配条件的属性并更新文档,如下所示:
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 }});
});