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

Can't use dot-notation for pushing to a deeply embedded array

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

      Mongoid can't handle $pushes into a deeply embedded array. Consider the following code

      class A
      include Mongoid::Document
      embeds_many :bs
      end

      class B
      include Mongoid::Document
      embedded_in :a, :inverse_of => :bs
      embeds_many :cs
      end

      class C
      include Mongoid::Document
      embedded_in :b, :inverse_of => :cs
      end

      c = C.create(:b => B.create(:a => A.create))

      which mongoid translates into this query:

      db.as.update(

      {"_id": "4c7ae50fc61aeb7190000028", "bs._id": "4c7ae50fc61aeb7190000029"}

      , {"$push": {"bs.cs":

      {"_id": "Whatever"}

      }})

      which fails with "can't append to array using string field name [cs]"

      Instead, mongoid should use the $ positional operator (http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator) like so:

      db.as.update(

      {"_id": "4c7ae50fc61aeb7190000028", "bs._id": "4c7ae50fc61aeb7190000029"}

      , {"$push": {"bs.$.cs":

      {"_id": "Whatever"}

      }})

            Assignee:
            Unassigned Unassigned
            Reporter:
            niels Niels Ganser
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: