Update value of array field using mongoid
<p>I'm trying to find a way to update a single array item using mongoid. I'm using Rails 4 and Mongodb.</p>

<p>My model looks like this</p>

<pre><code>class User
  include Mongoid::Document
  include Mongoid::Timestamps

field :my_book_list, type: Array, default: []
field :book_name, type: String
</code></pre>

<p>I'm able to add entry to the array field using the following code:</p>

<pre><code>User.where(id: self.user_id).add_to_set("my_book_list" =&gt; self.book_name)
</code></pre>

<p>After I have added data to the array, in the database it looks like this</p>

<pre><code>db.collection.users 
{
    "_id" : ObjectId("56e09d54a0d00b0528000001"),
    "status" : true,
    "sign_in_count" : 3,
    "my_book_list" : 
     ["Learning Ruby", "MongoDB for Dummies"]

}
</code></pre>

<p>What I'm struggling with is to find a Rails / Mongoid way of updating the value of an item in the array by looking for the name.</p>

<p>Simply put, how do I change the value of my_book_list[1] by searching for it through name and not knowing its index.  In this case index 1 is "MongoDB for Dummies" and needs to be updated to "MongoDB". So that the "my_book_list" array field looks like this after its updated:</p>

<pre><code>db.collection.users 
{
    "_id" : ObjectId("56e09d54a0d00b0528000001"),
    "status" : true,
    "sign_in_count" : 3,
    "my_book_list" : 
     ["Learning Ruby", "MongoDB"]

}
</code></pre>

<p>How do I achieve this ? </p>