MongoDB 更新查询以根据值取消设置特定数组成员
MongoDB update query to unset specific array members based on the value
我有一个子文档数组,如下所示
/* record 1 */
{
"_id" : "462044",
"program" : [
{
"name" : "Business Services"
},
{
"name" : "Homeland Security"
},
{
"title" : [
"associate"
],
"name" : "Engineering"
},
{
"title" : [
"associate"
],
"name" : "Computer Software"
}
]
}
/* record 2 */
{
"_id" : "462045",
"program" : [
{
"name" : "Business Services"
},
{
"name" : "Homeland Security"
},
{
"title" : [
"associate"
],
"name" : "Engineering"
},
{
"title" : [
"associate"
],
"name" : "Computer Software"
}
]
}
[我想删除没有 'title' 个子成员的数组成员]
如何编写 mongo 更新查询或 nodejs 函数来获取以下文档集
/* record 1 */
{
"_id" : "462044",
"program" : [
{
"title" : [
"associate"
],
"name" : "Engineering"
},
{
"title" : [
"associate"
],
"name" : "Computer Software"
}
]
}
/* record 2 */
{
"_id" : "462045",
"program [
{
"title" : [
"associate"
],
"name" : "Engineering"
},
{
"title" : [
"associate"
],
"name" : "Computer Software"
}
]
}
嗯,首先你的文档结构看起来很奇怪。每条记录都有一个不同的字段,代表基本相同的信息,在您的情况下 school1
和 school2
.
如果您将这些字段重命名为 school
.
,则可以轻松修复
如果您固定了文档结构,您可以借助聚合框架和普通更新查询轻松消除所需的子文档。
示例:
db.collection.aggregate([
{
$project: {
school: {
$filter: {
input: "$school.program",
as: "prog",
cond: {
$ne: [
{
$ifNull: [
"$$prog.title",
true
]
},
true
]
}
}
}
}
}
]).forEach(function(o){
db.collection.updateOne({
_id: o._id
},
{
$set: {
school: o.school
}
});
})
这会将您的旧文档变成(删除所有不包含 title
的子字段形式的程序:
{
"_id" : ObjectId("57371cdf81e9959ba1a5fab0"),
"school" : [
{
"title" : [
"associate"
],
"name" : "Engineering"
},
{
"title" : [
"associate"
],
"name" : "Computer Software"
}
]
}
{
"_id" : ObjectId("57371cdf81e9959ba1a5fab1"),
"school" : [
{
"title" : [
"associate"
],
"name" : "Engineering"
},
{
"title" : [
"associate"
],
"name" : "Computer Software"
}
]
}
我有一个子文档数组,如下所示
/* record 1 */ { "_id" : "462044", "program" : [ { "name" : "Business Services" }, { "name" : "Homeland Security" }, { "title" : [ "associate" ], "name" : "Engineering" }, { "title" : [ "associate" ], "name" : "Computer Software" } ] } /* record 2 */ { "_id" : "462045", "program" : [ { "name" : "Business Services" }, { "name" : "Homeland Security" }, { "title" : [ "associate" ], "name" : "Engineering" }, { "title" : [ "associate" ], "name" : "Computer Software" } ] }
[我想删除没有 'title' 个子成员的数组成员]
如何编写 mongo 更新查询或 nodejs 函数来获取以下文档集
/* record 1 */ { "_id" : "462044", "program" : [ { "title" : [ "associate" ], "name" : "Engineering" }, { "title" : [ "associate" ], "name" : "Computer Software" } ] } /* record 2 */ { "_id" : "462045", "program [ { "title" : [ "associate" ], "name" : "Engineering" }, { "title" : [ "associate" ], "name" : "Computer Software" } ] }
嗯,首先你的文档结构看起来很奇怪。每条记录都有一个不同的字段,代表基本相同的信息,在您的情况下 school1
和 school2
.
如果您将这些字段重命名为 school
.
如果您固定了文档结构,您可以借助聚合框架和普通更新查询轻松消除所需的子文档。
示例:
db.collection.aggregate([
{
$project: {
school: {
$filter: {
input: "$school.program",
as: "prog",
cond: {
$ne: [
{
$ifNull: [
"$$prog.title",
true
]
},
true
]
}
}
}
}
}
]).forEach(function(o){
db.collection.updateOne({
_id: o._id
},
{
$set: {
school: o.school
}
});
})
这会将您的旧文档变成(删除所有不包含 title
的子字段形式的程序:
{
"_id" : ObjectId("57371cdf81e9959ba1a5fab0"),
"school" : [
{
"title" : [
"associate"
],
"name" : "Engineering"
},
{
"title" : [
"associate"
],
"name" : "Computer Software"
}
]
}
{
"_id" : ObjectId("57371cdf81e9959ba1a5fab1"),
"school" : [
{
"title" : [
"associate"
],
"name" : "Engineering"
},
{
"title" : [
"associate"
],
"name" : "Computer Software"
}
]
}