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

3.0.0.rc atomic update with update_all issue

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

      # MODEL
      class User
        include Mongoid::Document
      
        field :username
        field :counter, :default => 0
        field :loves, :default => []
      
        index({ :username => 1 })
      end
      
      # DB SEED FILE
      marc = User.new
      marc.username = 'marc'
      marc.counter = 5
      marc.loves = ['a', 'b', 'c']
      marc.save
      
      # QUERY
      User.where(:username => 'marc').
                                update_all(
                                  "$inc" => {
                                    :counter => -1,
                                  },
                                  "$pull" => {
                                    :loves => 'b'
                                  }
                                )
      
      # RESULT IN DB
      {
         "$inc": {
           "counter": -1
        },
         "$pull": {
           "loves": "b"
        },
         "_id": ObjectId("4fe08719bd31e0fbd7000001"),
         "counter": 5,
         "loves": {
           "0": "a",
           "1": "b",
           "2": "c"
        },
         "username": "marc"
      }
      
      

      I've created a repository for this example if it helps:
      https://github.com/marbemac/rails-sandbox

      Just run the db seed file and then hit the root url and you should see the behavior.

            Assignee:
            durran Durran Jordan
            Reporter:
            marbemac Marc MacLeod
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: