0

I use MongoID and PostgreSQL on Rails 4 and I have a notification system.

Each user has one notification document. I would like to be able to mark all as read in a controller. Here is my notification schema :

{
  "_id" : ObjectId("53e360156d6163513e000000"),
  "unread_counter" : 1,
  "user_id" : 1,
  "rows" : [
    {
      "_id" : ObjectId("53e369166d61635328000000"),
      "readed" : false,
      "created_at" : ISODate("2014-08-07T11:55:02.936Z"),
      "author" : {
        "_id" : ObjectId("53e369166d61635328010000"),
        "username" : "edouard",
        "user_id" : 6
      },
      "subject" : {
        "_id" : ObjectId("53e369166d61635328020000"),
        "type" : "follower"
      }
    }
  ]
}

The problem is that elem_match just match one element in rows array.

Notification.where(:user_id => current_user.id)
.elem_match(rows: { readed: false })
.update_all("$set" => {"rows.$.readed" => true })

I have been inspired by this post : mongoid update elements within array

How can I match all "readed" : false row elements and then update them to true ?

Community
  • 1
  • 1
Nicolas Vergoz
  • 169
  • 1
  • 10

0 Answers0