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

Atomic increments on embedded documents do not work

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

      I'm trying to use inc on an embedded document, and it seems like the generated mongo command does not actually perform anything and the increment is lost.
      To recreate the issue:
      class User
      include Mongoid::Document
      embeds_many :game_stats
      end
      class GameStat
      include Mongoid::Document
      embedded_in :user
      field :playcount, :type=>Integer, :default=>0
      end

      >> u=User.first
      >> u.game_stats.create
      >> u.save
      >> u.game_stats.first.inc(:playcount, 1)
      => 1

      Checking playcount again shows it did not in fact increment.

      the mongo console shows the following command was sent:
      mydbname.game_stats query:

      { _id: ObjectId('4d9b073e3149bc1124000002'), game_stats._id: ObjectId('4d9d7ce53149bc197c000002') }

      update: { $inc:

      { playcount: 1 }

      } 1ms

      Seems like it should be using $inc:

      {game_stats.0.playcount : 1}

      , as it does with a regular save() call:
      >> u.game_stats.first.playcount += 1
      >> u.save
      which translates into:
      update mydbname.users query:

      { _id: ObjectId('4d9b073e3149bc1124000002') }


      update: { $set:

      { game_stats.0.playcount:1 }

      }

            Assignee:
            durran Durran Jordan
            Reporter:
            ehudros ehudros
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: