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

Embedded document relation ID inconsistency, String vs. ObjectId

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

      hi All,

      I am using mongoid 3.1.4, and have run into an issue with how the ID of a relation from an embedded document to a top-level document is persisted to mongodb.

      Let there be top-level documents Foo and Bar, and then an embedded document SubBar defined as follows.

      class Foo
        include Mongoid::Document
      end
      
      class Bar
        include Mongoid::Document
        embeds_many :sub_bars
      end
      
      class SubBar
        include Mongoid::Document
        belongs_to :foo, inverse_of: nil
        embedded_in :bar
      end
      

      In the app, I have the line:

      foo = Foo.new.save!
      bar = Bar.new
      bar.profiles.build(foo_id: foo.id)
      bar.save!
      

      The result is a mongodb document (faking the object ids below) in the bars collection

      {_id: ObjectId("1"), sub_bars: [{_id: ObjectId("2"), foo_id: "3"}]}
      

      However, running the same command in a rails c session produces a mongodb document in the bars collection like

      {_id: ObjectId("1"), sub_bars: [{_id: ObjectId("2"), foo_id: ObjectId("3")}]}
      

      I am wondering why in one case foo_id is a string, whereas in the second case, foo_id is an ObjectId. I would prefer the result be an ObjectId.

      Thanks!
      -Eric

      PS - Note that the MOPED logs show the same $pushAll in the update command in both cases.

      "$pushAll" => {"sub_bars" => [{_id => "1", foo_id => "3"}]}
      

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            ethul ethul
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: