Javascript 如何使用Mongoose更新数组中对象的字段
我需要按id查找一个房间,并将received字段更新为true,其中用户id等于xx 文件如下:Javascript 如何使用Mongoose更新数组中对象的字段,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我需要按id查找一个房间,并将received字段更新为true,其中用户id等于xx 文件如下: { "_id" : ObjectId("5e5d0d870fc69641a41a3c65"), "users" : [ ObjectId("5e57d64d92cc878760086980"), ObjectId("5e57d64592cc87876008697e") ], "messages" : [ {
{
"_id" : ObjectId("5e5d0d870fc69641a41a3c65"),
"users" : [
ObjectId("5e57d64d92cc878760086980"),
ObjectId("5e57d64592cc87876008697e")
],
"messages" : [
{
"_id" : ObjectId("5e67834b6c8b2d356a4ad9fd"),
"text" : "Hello",
"user" : ObjectId("5e57d64d92cc878760086980"),
"createdAt" : ISODate("2020-03-10T12:08:43.006Z"),
"sent" : true,
"received" : false
},
{
"_id" : ObjectId("5e6783076c8b2d356a4ad9fc"),
"text" : "Hello",
"user" : ObjectId("5e57d64d92cc878760086980"),
"createdAt" : ISODate("2020-03-10T12:07:35.544Z"),
"sent" : true,
"received" : true
}
],
"createdAt" : ISODate("2020-03-02T13:43:35.522Z"),
"updatedAt" : ISODate("2020-03-10T12:08:43.006Z"),
"unReads" : {
"5e57d64d92cc878760086980" : 1,
"5e57d64592cc87876008697e" : 5
},
"__v" : 0
}
****看起来你的帖子大部分都是代码;请添加更多详细信息。****您需要:
你能出示你要更新的文档吗?@mickl更新了帖子
Model.update({_id: ObjectId("5e5d0d870fc69641a41a3c65"), "messages.user": ObjectId("5e57d64d92cc878760086980")}, { $set: { "messages.$.received": true } })