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

update_all with non existing field

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      I have problem when I am trying to use update_all method on model with non existing field.

      My User model is -

      class User
      include Mongoid::Document
      field :name, type: String
      end

      NOTE: Here I have not included 'Mongoid::Attributes::Dynamic'

      When I am trying update_all method on non existing field it is creating field on mongodb

      2.1.3 :001 > User.all.to_a
      => #<User _id: 54e855837368773ff7000000, name: "Shweta">, #<User _id: 54e855877368773ff7010000, name: "Anuja">

      2.1.3 :006 > User.where(name: 'Shweta').update_all(last_name: 'Kale')
      =>

      {"updatedExisting"=>true, "n"=>1, "connectionId"=>20, "err"=>nil, "ok"=>1.0}

      2.1.3 :007 > User.all.to_a

      => #<User _id: 54e855877368773ff7010000, name: "Anuja">, #<User _id: 54e855837368773ff7000000, name: "Shweta">

      Documents from mongo shell

      db.users.find();

      { "_id" : ObjectId("54e855877368773ff7010000"), "name" : "Anuja" } { "_id" : ObjectId("54e855837368773ff7000000"), "last_name" : "Kale", "name" : "Shweta" }

      It has created field last_name in mongodb

      When I am trying to use update_attributes on user, its giving UnknownAttribute Error

      2.1.3 :014 > User.last.update_attributes(last_name: 'Kale')
      Mongoid::Errors::UnknownAttribute:

      Is this bug or I am missing something?

            Assignee:
            Unassigned Unassigned
            Reporter:
            Shwetakale Shwetakale [X]
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: