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

Mongoid 7.4 inserts only one embedded document when multiple documents are created with nil _id

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 8.0.1
    • Affects Version/s: 7.4.0
    • Component/s: None
    • Labels:
      None
    • Minor Change

      After upgrading from mongoid 7.3.4 to 7.4.0 we saw one of our specs stating to fail. The issue boils down to the following code working with the previous version but being broken by v7.4.

      class Garden
        include Mongoid::Document
        embeds_many :carots
      
        def with_carots(carots_list)
          carots_list.each do |carot_data|
            carot = carots.find_or_initialize_by(name: carot_data[:name])
            carot.id = carot_data[:id]
          end
        end
      end
      
      class Carot
        include Mongoid::Document
        field :name
        embedded_in :garden
      end
      
      g = Garden.create
      g.with_carots([{ name: 'C1' }, { name: 'C2' }])
      g.save
      

      In v7.3.4 the code above would produce the following update query:

      {
        "update" => "gardens",
        "ordered" => true,
        "updates" => [
          {
            "q" => { "_id" => BSON::ObjectId('XXX') },
            "u" => {
              "$push" => {
                "carots" => {
                  "$each" => [
                    {"_id" => nil, "name" => "C1" },
                    {"_id" => nil, "name" => "C2" }
                  ]
                }
              }
            }
          }
        ]
      }
      

      but using mongoid v7.4.0 it now produces the following one:

      {
        "update" => "gardens",
        "ordered" => true,
        "updates" => [
          {
            "q" => { "_id" => BSON::ObjectId('XXX') },
            "u" => {
              "$push" => {
                "carots" => {
                  "$each" => [
                    { "_id" => nil, "name" => "C1" }
                  ]
                }
              }
            }
          }
        ]
      }
      

      We were able to fix the issue by updating the code to

      carot.id = carot_data[:id] if carot_data[:id]
      

      but it seems like a regression as it broke our code while upgrading to a minor version.

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            simon@textmaster.com Simon Courtois
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: