Node.js FeathersJS Mongoose:更新匹配的子文档不起作用
我想更新子文档的值,其中消息具有特定id,并且用户id位于收件人数组中。我想用指定的用户id更新匹配对象的值 当我在MongoDB CLI上运行以下查询时,一切正常,值也会更新:Node.js FeathersJS Mongoose:更新匹配的子文档不起作用,node.js,mongodb,mongoose,feathersjs,Node.js,Mongodb,Mongoose,Feathersjs,我想更新子文档的值,其中消息具有特定id,并且用户id位于收件人数组中。我想用指定的用户id更新匹配对象的值 当我在MongoDB CLI上运行以下查询时,一切正常,值也会更新: db.getCollection('messages').update({ _id : ObjectId("57d7edb8c497a75a6a7fde60"), "recipients.userId" : "5789127ae2bcc79326462dbc" },{ $set : {"recipients
db.getCollection('messages').update({
_id : ObjectId("57d7edb8c497a75a6a7fde60"),
"recipients.userId" : "5789127ae2bcc79326462dbc"
},{
$set : {"recipients.$.read": true}
});
但当我在FeatherJS应用程序中通过JS运行以下查询时:
messageService.update({
_id : '57d7edb8c497a75a6a7fde60',
"recipients.userId" : "5789127ae2bcc79326462dbc"
},{
$set: {"recipients.$.read": true}
}).then(function(e) {
console.log(e)
}).catch(function(e) {
console.log(e);
});
我得到一个错误:
GeneralError:位置运算符未从查询中找到所需的匹配项。未展开的更新:收件人。$.read
我做错了什么
有没有更好的方法一次更新多条消息?
谢谢 要通过查询更新一条或多条记录,您需要通过将
id
设置为null
并将查询放入参数来调用。query
:
messageService.update(null, {
$set: {"recipients.$.read": true}
}, {
query: {
_id : '57d7edb8c497a75a6a7fde60',
"recipients.userId" : "5789127ae2bcc79326462dbc"
}
}).then(function(e) {
console.log(e)
}).catch(function(e) {
console.log(e);
});
要通过查询更新一条或多条记录,您需要通过将
id
设置为null
并将查询放入params来调用。query
:
messageService.update(null, {
$set: {"recipients.$.read": true}
}, {
query: {
_id : '57d7edb8c497a75a6a7fde60',
"recipients.userId" : "5789127ae2bcc79326462dbc"
}
}).then(function(e) {
console.log(e)
}).catch(function(e) {
console.log(e);
});
这对我来说是有意义的,但是当我运行这段代码时,我会得到一个“Error:[object]”响应。这可能有助于获得更好的错误消息(可能是一个bug,它没有正确地转换错误)。@Daff谢谢你救了我一天!这对我来说是有意义的,但是当我运行这段代码时,我会得到一个“Error:[object]”响应。这可能有助于获得更好的错误消息(可能是一个bug,它没有正确地转换错误)。@Daff谢谢你救了我一天!