1

I have following two Mongoid documents:

class Book
  include Mongoid::Document
  embeds_many :pages
end

class Page
  include Mongoid::Document
  embedded_in :book
  field :reviewer, type: String
  field :done, type: Boolean
end

And I created two Book documents with four Page documents embedded:

reviewers = %w(Alice Bob Charlie)

2.times do
  book = Book.new
  4.times do
    reviewer = reviewers.sample
    book.pages << Page.new(reviewer: reviewer, done: false)
  end
  book.save
end

Note that the reviewer is chosen from reviewers randomly.

I want to set done field to true for all Page documents reviewed by 'Alice' at a time. How can I do it?

I wrote following code:

Book.where('pages.reviewer' => 'Alice').set('pages.$.done' => true)

This update only one Page document for each Book document even if several pages are reviewed by 'Alice'.

I use mongoid 4.0.0.alpha1.

Tsutomu
  • 4,848
  • 1
  • 46
  • 68
  • Thanks everyone for pointing me to a relevant answer. The short answer to my question is "You cannot do that", isn't it? – Tsutomu Jan 13 '14 at 13:25

0 Answers0