【问题标题】:Mongodb positional operator is not workingMongodb位置运算符不起作用
【发布时间】:2021-09-18 16:31:33
【问题描述】:

当我试图只更新“标题”时,它工作正常,但如果我试图更新数组中的嵌套对象,它就不起作用。

数据: Data

工作:

const restaurant = await Restaurant.update(
    {_id: '60e6828e4992a2979fa0ba3e'},
    {$set: {title: "Hello}},
  );

不工作:

const restaurant = await Restaurant.update(
    {_id: '60e6828e4992a2979fa0ba3e', 'grades.grade': 85},
    {$set: {'grades.$.grade': 0}},
  );

【问题讨论】:

标签: javascript node.js reactjs mongodb react-native


【解决方案1】:

演示 - https://mongoplayground.net/p/caFBz-lx8dQ

使用$[]

过滤的位置运算符 $[] 识别与更新操作的 arrayFilters 条件匹配的数组元素

db.collection.update(
 {},
 { $set: { "grades.$[elem].grade": 0 }},
 { arrayFilters: [ {"elem.grade": 85 } ]}
)

【讨论】:

  • 这有点奇怪。您的代码仍然无法在我的项目中运行。自从我使用它以来,我认为猫鼬有问题。
猜你喜欢
  • 2021-07-26
  • 2018-04-27
  • 1970-01-01
  • 2015-01-25
  • 1970-01-01
  • 1970-01-01
  • 2022-08-20
  • 2021-08-28
  • 1970-01-01
相关资源
最近更新 更多