mongodb - 使用 SailsJS 更新 MongoDb 中的对象数组

标签 mongodb sails.js waterline

我在 MongoDB 中有一条记录,格式如下:

  {
    attachment: 'xxxxxxx',
    createdAt: TueAug04201514: 52: 23GMT+0400(GST),
    text: 'xxxxxxx',
    sender: '55784247c95c59cc746904e1',
    status: [
      {
        user: '5589464675fc0892757d170e',
        is_read: 0
      },
      {
        user: '55a4a39d5c0bf23d21e6c485',
        is_read: 0
      },
      {
        user: '55a4a38e5c0bf23d21e6c483',
        is_read: 0
      },
      {
        is_read: 0,
        user: '55784247c95c59cc746904e1'
      }
    ],
    thread: '55c0946b80320adb3fd04d0e',
    updatedAt: TueAug04201515: 45: 55GMT+0400(GST),
    id: '55c09967708b73184558fbc8'
  }

我想更新 status 数组中的 is_read。这是我尝试过的。

      Messages.update({
          thread: threadIds,
          status: { $elemMatch: { user: user.id, is_read: 0 } }
        },
        { $set: { "status.$.is_read": 1 } })

但是相同的查询在 mongodb shell 中运行得非常好。如果我遗漏了什么,谁能指导我?顺便说一下,我在使用 Waterline ORMSailsJS 中运行此查询。

最佳答案

要使用与 mongo shell 相同的查询,您必须使用 Model.native因为 Sails 基于 Waterline,它的查询语言与 Mongo 略有不同。

我还没有尝试过这个,但我敢打赌这应该可以做到:

  Messages.update({
      thread: threadIds,
      'status.user': user.id,
      'status.is_read': 0 
  },
  { 'status.is_read': 1 }, callback);

关于mongodb - 使用 SailsJS 更新 MongoDb 中的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31808811/

相关文章:

MongoDB - 将简单字段更改为对象

mongodb - Mongoose 的 $or 条件的 find 方法无法正常工作

javascript - Sails.js Waterline 查询(按关联)

node.js - sails 钩航线禁止

node.js - 使用网络服务器外部模型的 Sails js

node.js - Mongoose 居住在 child 中

mongodb - 正确使用 mongodb ScopedDbConnection?

node.js - 如何获取解码后的 JSON Web Token (JWT) 的值

javascript - 如何检测某个对象是否是 Sails.js 中的 Waterline 模型或模型集合

sql - MSSQL : Finding all items between two dates