【问题标题】:FeathersJS Mongoose: Updating matched sub-document not workingFeathersJS Mongoose:更新匹配的子文档不起作用
【发布时间】:2016-09-18 12:46:48
【问题描述】:

我想更新子文档的值,其中消息具有特定 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:位置运算符未从查询中找到所需的匹配项。未扩展更新:收件人.$.read

我做错了什么?

有没有更好的方法一次更新多条消息?

谢谢!

【问题讨论】:

    标签: node.js mongodb mongoose feathersjs


    【解决方案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);
    });
    

    【讨论】:

    • 对我来说很有意义,但是当我运行此代码时,我得到一个“错误:[object Object]”响应
    • 也许docs.feathersjs.com/debugging/readme.html 可以帮助获得更好的错误消息(可能是它没有正确转换错误的错误)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 2017-03-17
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    相关资源
    最近更新 更多