3

I have a record in MongoDB in the following format:

  {
    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'
  }

I would like to update is_read inside status array. Here is what I have tried.

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

But same query runs really good in mongodb shell. Can anyone guide me if I am missing something? By the way I am running this query in SailsJS that uses Waterline ORM.

2
  • What kind of relation do you have between the Message model and its status attribute? Commented Aug 4, 2015 at 13:15
  • I couldn't understand what relation are you referring. status is an attribute of Message model with type array Commented Aug 4, 2015 at 13:23

2 Answers 2

3

To use the "same" query as the mongo shell, you'll have to use Model.native since Sails is based on Waterline, which has a slightly different query language from Mongo.

I haven't tried this, but I'd wager that this should do it:

  Messages.update({
      thread: threadIds,
      'status.user': user.id,
      'status.is_read': 0 
  },
  { 'status.is_read': 1 }, callback);
Sign up to request clarification or add additional context in comments.

3 Comments

Yes you are right, I solved this using Model.native but your posted code is wrong.
I'm glad you did. As I said, I hadn't tried the code. Can you tell me what is wrong so I can update it?
'status.user': user: user.id, here is a syntax error. There can only be one key and its pair.
0

So here is how I solved this.

     // You have to use {model_name}.mongo.objectId() for ids otherwise your
     // query will not work.
     threadIds.push(Messages.mongo.objectId(thread.id));

     Messages.native(function (err, collection ) {

        if (err) {
          return res.send(500, err);
        }

        collection.update({
            thread: {$in: threadIds},
            status: {
              $elemMatch: {
                user: user.id,
                is_read: 0
              }
            }
          },
          {
            $set: {"status.$.is_read": 1}
          }, function (err, messages) {

            if (err) {
              return res.send(500, err);
            }

            return res.send(200);

          });

      });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.