Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2423

#update_all erases embedded document except for what's in the update

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.7
    • Affects Version/s: None
    • Component/s: None

      When doing model.children.where(...).update_all(:foo => :bar), Mongoid erases the child except for what's in the update.

      class Book
        include Mongoid::Document
        embeds_many :pages
      end
      
      class Page
        include Mongoid::Document
        embedded_in :book
        field :text
        field :number, :type => Integer
        field :read, :type => Boolean
      end
      
      b = Book.create
      b.pages.create(:text => "ABC", :read => true, :number => 1)
      b.pages.create(:text => "DEF", :read => true, :number => 2)
      b.pages.create(:text => "GEH", :read => false, :number => 3)
      
      b.reload
      b.pages
      
       # => [#<Page _id: 506a03b9dd365a05d6000002, text: "ABC", number: 1, read: true>, #<Page _id: 506a03b9dd365a05d6000003, text: "DEF", number: 2, read: true>, #<Page _id: 506a03b9dd365a05d6000004, text: "GEH", number: 3, read: true>] 
      b.pages.where(:read => false).update_all(:read => true)
      
      # Reload the model and check the pages
      # The third page will have nil text and number
      b.reload
      b.pages
       # => [#<Page _id: 506a03b9dd365a05d6000002, text: "ABC", number: 1, read: true>, #<Page _id: 506a03b9dd365a05d6000003, text: "DEF", number: 2, read: true>, #<Page _id: 506a03cddd365a05d6000005, text: nil, number: nil, read: true>] 
      

      Moped query:

      Mon Oct 1 16:57:35 [conn2] update development.books query:

      { _id: ObjectId('506a03b9dd365a05d6000001') }

      update: { $set: { pages.2:

      { read: true }

      } } idhack:1 0ms

      Using Mongoid master, 9aa7e2975877dff848c026449c243d195ccea436.

            Assignee:
            durran Durran Jordan
            Reporter:
            jonhyman Jon Hyman
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: