javascript - 更新嵌套字段中的 value ?

user :["id": 1, userObj:[{"bookid": 1, "library":"Oxford", "taken":true}, {"bookid": 2, "library":"Cambridge", "taken":true}]]

我想更新 value 并将其设置为 false 的 bookid。

tmp = await User.findOneAndUpdate({id: req.body.id},  { $elemMatch: {'userObj.bookid': req.body.bookid }, { $set: { 'userObj.taken': false }, }, { new: true } )

回答1

您需要使用位置运算符

db.collection.update({
  id: 1,
  "userObj.bookid": 1
},
{
  $set: {
    "userObj.$.taken": false //$ is a positional operator which updates the matching array element
  },
  
})

https://mongoplayground.net/p/55DYX6Mkopa

相似文章

c - GPS数据处理

NMEA-0183协议是美国国家海洋电子协会(NMEA)为了在不同的GPS(全球定位系统)导航设备之间建立统一的BTCM(MarineRadioTechnicalCommittee)标准而制定的一套通...

随机推荐

最新文章