如何从 MongoDB 中的 JSON 中删除空数组。我的对象中有多个数组。
db.runCommand({
update: "table",
updates: [
{
q: {_id: { $in: ['id1', 'id2']}},
u: {
$pull: { "a.b" : { "a.$[].b.$[].c" : { $exists: true, $size: 0 } }}
}
}
]
})
它只告诉空数组,但不修改它。
最佳答案
你已经快到了,但是你需要使用$unset而不是$pull,因为pull只是从数组中删除数据,但unset会删除数组本身。
db.runCommand({
update: "table",
updates: [
{
q: {_id: { $in: ['id1', 'id2']}},
u: {
$unset: { "a.b" : { "a.$[].b.$[].c" : { $exists: true, $size: 0 } }}
}
}
]
})
关于arrays - 如何从 json 中删除空数组。我数组中没有元素,我想删除数组。它的嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57711010/