FeathersJS Mongoose:更新匹配的子文档无法正常工作

时间:2016-09-18 12:46:48

标签: node.js mongodb mongoose feathersjs

我想更新子文档的值,其中邮件具有特定ID,并且用户ID在收件人数组中。我想用指定的用户ID更新匹配对象的值。

当我在MongoDB CLI上运行以下查询时,一切正常并且值已更新:

db.getCollection('messages').update({
  _id : ObjectId("57d7edb8c497a75a6a7fde60"),
  "recipients.userId" : "5789127ae2bcc79326462dbc"
},{
  $set : {"recipients.$.read": true}
});

但是当我在FeathersJS应用程序中通过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:位置运算符未找到查询所需的匹配项。未更新的更新:收件人。$。阅读

我做错了什么?

是否有更好的方法可以同时更新多条消息?

谢谢!

1 个答案:

答案 0 :(得分:1)

要根据查询更新一个或多个记录,您需要通过将id设置为null并将查询放入params.query来调用service method

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);
});
相关问题