【问题标题】:mongodb (pymongo) update all values inside a nested arraymongodb (pymongo) 更新嵌套数组中的所有值
【发布时间】:2020-12-24 16:35:09
【问题描述】:

我是我的 mongoDB,我有以下数据:

{"_id":{"$oid":"5f3c3595271ef86d69a2909f"}
 data:{
 arr1: [ {arr2:[{a:[1 2 3],b:7},
                {a:[4 5 6],b:7},
                {a:[0 8 7],b:7},
                {a:[2 2 3],b:7}},
         {arr2:[{a:[1 2 3],b:7},
                {a:[4 5 6],b:7},
                {a:[0 8 7],b:7},
                {a:[2 2 3],b:7}},
         {arr2:[{a:[1 2 3],b:7},
                {a:[4 5 6],b:7},
                {a:[0 8 7],b:7},
                {a:[2 2 3],b:7}},
         {arr2:[{a:[1 2 3],b:7},
                {a:[4 5 6],b:7},
                {a:[0 8 7],b:7},
                {a:[2 2 3],b:7}},
         {arr2:[{a:[1 2 3],b:7},
                {a:[4 5 6],b:7},
                {a:[0 8 7],b:7},
                {a:[2 2 3],b:7}}]}}

我想从代码中的任何地方删除 / 清空 a。 我试过类似的东西:

...update({},{"$set":{"data.arr1.$[].arr2.${}.a":[]},multi=True)

没有用。

我该怎么做? 谢谢!

【问题讨论】:

    标签: python mongodb pymongo pymongo-3.x


    【解决方案1】:

    你差不多了,看起来你可能打错了。这里:

    update({},{"$set":{"data.arr1.$[].arr2.${}.a":[]},multi=True)
    

    ${}arr2 之后,这也应该是$[]。如果你改变它,它会起作用。更新文档将如下所示:

    update({},{"$set":{"data.arr1.$[].arr2.$[].a":[]},multi=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2021-11-30
      • 2015-08-27
      • 2020-09-15
      • 2015-02-19
      • 1970-01-01
      • 2011-05-06
      相关资源
      最近更新 更多