猫鼬:使用$ pull删除嵌入式文档数组中的值(MongoDB 3.4版本)

安舒尔·贾恩(Anshul Jain)

我试图在节点js中使用猫鼬从嵌套数组中删除值。我的架构结构是这样的:

    ([{_id: 1,


    results: [
      { item: "A", score: 5, answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ] },
      { item: "B", score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 9 } ] }
   ]
},
{
   _id: 2,
   results: [
      { item: "C", score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 7 } ] },
      { item: "B", score: 4, answers: [ { q: 1, a: 0 }, { q: 2, a: 8 } ] }
   ]
}])

我想从a为8的答案中删除值。我的查询是

db.collection.update({"results.item":{$in:["C","B"]}},{$pull:{"results.$.answers":{a:8}}})

此查询工作正常,但仅更新一个文档。请帮忙。

我想要的输出是

([{
   _id: 1,
   results: [
      { item: "A", score: 5, answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ] },
      { item: "B", score: 8, answers: [ { q: 2, a: 9 } ] }
   ]
},
{
   _id: 2,
   results: [
      { item: "C", score: 8, answers: [ { q: 2, a: 7 } ] },
      { item: "B", score: 4, answers: [ { q: 1, a: 0 } ] }
   ]
}])

提前致谢。

尼尔·伦恩

您需要一条语句从MongoDB 3.6或更高版本中过滤掉位置$[<identifier>]支持:

db.collection.updateMany(
  { "results.item": { "$in": ["C","B"] } },
  { "$pull": { "results.$[el].answers": { "a": 8 } } },
  { "arrayFilters":  [{ "el.item": { "$in": ["C", "B"] } }] }
)

结果是:

{
        "_id" : 1,
        "results" : [
                {
                        "item" : "A",
                        "score" : 5,
                        "answers" : [
                                {
                                        "q" : 1,
                                        "a" : 4
                                },
                                {
                                        "q" : 2,
                                        "a" : 6
                                }
                        ]
                },
                {
                        "item" : "B",
                        "score" : 8,
                        "answers" : [
                                {
                                        "q" : 2,
                                        "a" : 9
                                }
                        ]
                }
        ]
}
{
        "_id" : 2,
        "results" : [
                {
                        "item" : "C",
                        "score" : 8,
                        "answers" : [
                                {
                                        "q" : 2,
                                        "a" : 7
                                }
                        ]
                },
                {
                        "item" : "B",
                        "score" : 4,
                        "answers" : [
                                {
                                        "q" : 1,
                                        "a" : 0
                                }
                        ]
                }
        ]
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用D3版本4的表格中的数据加载问题

使用猫鼬节点JS获取3模式值

d3版本4中的d3.layout.treemap

d3版本4中的“ d3.svg.axis()”是什么?

如何在操作应该是原子的猫鼬中更新数组中的嵌入式文档,应更新最新版本并返回更新的文档?

使用D3版本4进行水平缩放?

在d3版本4中按序数比例创建散点图

D3版本4中的单元测试过渡

猫鼬嵌入式方案不能从嵌入式数组中删除

d3版本4中未触发d3-transition attrTween,styleTween方法

猫鼬:如何更新子嵌入式文档数组?

复杂查询猫鼬-嵌入式文档数组

如何将ionic 3和ionic 4版本同时保存在我的机器中

如何使用Mongoose将值推入MongoDB中嵌入式文档中的数组?

通过与猫鼬中的其他数组匹配从嵌入式数组中删除元素

猫鼬对嵌入式文档数组中的某些元素进行计数

猫鼬-不推荐使用[email protected]:请升级到2.2.19或更高版本?

如何使用将在python2和python3版本中兼容的六模块迁移代码

如何使用猫鼬调用MongoDB数组

如何在猫鼬中使用$ set动态更新嵌入式文档

在Google Drive API的v3版本中通过drive.files.copy转换通过nodejs使用API将Word文档转换为Google文档

如何使用猫鼬跳过文档?

MongoDB删除3级嵌入式文档

在Eclipse中的Android 5.0版本中使用CardView

使用猫鼬的mongodb连接超时

MongoDB猫鼬使用地图查找

如何使用猫鼬查找数组元素?

无法使用猫鼬创建数组

猫鼬在find()。where()中使用数组