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

Mongoid upsert triggers an bson_type error for nested documents

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 6.0.0, 6.1.0
    • Component/s: None
    • Labels:
      None
    • Environment:
      Rails 5.0.2
      macOS Sierra
      MongoDB 3.2.8

      Ahoy is a tracking library that has an automatic mode that will persist tracking data to a Mongoid Store.

      The tracking seems to fail when attempting to track models with nested documents. I have the following error

      > undefined method `bson_type' for #<Payment::Subscription:0x007f90a95cd5f0>

      Where Payment::Subscription is a Mongoid document embedded_in a polymorphic company document

      The first call to the Mongoid library is a call to an #upsert method, which after a long series of calls triggers the final error

      For reference here is my embedded document

      {{module Payment
      class Subscription
      include Mongoid::Document
      include Mongoid::Timestamps
      include Mongoid::Attributes::Dynamic

      embedded_in :subscribable, polymorphic: true
      ...
      end }}

      And here is my trace

      {{mongoid (6.1.0) lib/mongoid/attributes/dynamic.rb:136:in `method_missing'
      bson (4.2.1) lib/bson/hash.rb:44:in `block in to_bson'
      bson (4.2.1) lib/bson/hash.rb:43:in `each'
      bson (4.2.1) lib/bson/hash.rb:43:in `to_bson'
      bson (4.2.1) lib/bson/hash.rb:46:in `block in to_bson'
      bson (4.2.1) lib/bson/hash.rb:43:in `each'
      bson (4.2.1) lib/bson/hash.rb:43:in `to_bson'
      bson (4.2.1) lib/bson/hash.rb:46:in `block in to_bson'
      bson (4.2.1) lib/bson/hash.rb:43:in `each'
      bson (4.2.1) lib/bson/hash.rb:43:in `to_bson'
      bson (4.2.1) lib/bson/array.rb:49:in `block in to_bson'
      bson (4.2.1) lib/bson/array.rb:46:in `each'
      bson (4.2.1) lib/bson/array.rb:46:in `each_with_index'
      bson (4.2.1) lib/bson/array.rb:46:in `to_bson'
      bson (4.2.1) lib/bson/hash.rb:46:in `block in to_bson'
      bson (4.2.1) lib/bson/hash.rb:43:in `each'
      bson (4.2.1) lib/bson/hash.rb:43:in `to_bson'
      mongo (2.4.1) lib/mongo/protocol/serializers.rb:163:in `serialize'
      mongo (2.4.1) lib/mongo/protocol/message.rb:213:in `block in serialize_fields'
      mongo (2.4.1) lib/mongo/protocol/message.rb:201:in `each'
      mongo (2.4.1) lib/mongo/protocol/message.rb:201:in `serialize_fields'
      mongo (2.4.1) lib/mongo/protocol/message.rb:104:in `serialize'
      mongo (2.4.1) lib/mongo/server/connection.rb:193:in `block in write'
      mongo (2.4.1) lib/mongo/server/connection.rb:192:in `each'
      mongo (2.4.1) lib/mongo/server/connection.rb:192:in `write'
      mongo (2.4.1) lib/mongo/server/connection.rb:162:in `deliver'
      mongo (2.4.1) lib/mongo/server/connection.rb:108:in `block in dispatch'
      mongo (2.4.1) lib/mongo/monitoring/publishable.rb:47:in `publish_command'
      mongo (2.4.1) lib/mongo/server/connection.rb:107:in `dispatch'
      mongo (2.4.1) lib/mongo/operation/write/command/writable.rb:39:in `block in execute'
      mongo (2.4.1) lib/mongo/server/connection_pool.rb:107:in `with_connection'
      mongo (2.4.1) lib/mongo/server.rb:242:in `with_connection'
      mongo (2.4.1) lib/mongo/operation/write/command/writable.rb:38:in `execute'
      mongo (2.4.1) lib/mongo/operation/write/write_command_enabled.rb:57:in `execute_write_command'
      mongo (2.4.1) lib/mongo/operation/write/write_command_enabled.rb:41:in `execute'
      mongo (2.4.1) lib/mongo/collection/view/writable.rb:242:in `block in update'
      mongo (2.4.1) lib/mongo/retryable.rb:112:in `write_with_retry'
      mongo (2.4.1) lib/mongo/collection/view/writable.rb:235:in `update'
      mongo (2.4.1) lib/mongo/collection/view/writable.rb:209:in `update_one'
      mongoid (6.1.0) lib/mongoid/query_cache.rb:182:in `update_one_with_clear_cache'
      mongoid (6.1.0) lib/mongoid/persistable/upsertable.rb:24:in `block in upsert'
      mongoid (6.1.0) lib/mongoid/persistable/upsertable.rb:47:in `block in prepare_upsert'
      activesupport (5.0.2) lib/active_support/callbacks.rb:97:in `_run_callbacks_'
      activesupport (5.0.2) lib/active_support/callbacks.rb:750:in `_run_upsert_callbacks'
      activesupport (5.0.2) lib/active_support/callbacks.rb:90:in `run_callbacks'
      mongoid (6.1.0) lib/mongoid/interceptable.rb:138:in `run_callbacks'
      mongoid (6.1.0) lib/mongoid/persistable/upsertable.rb:46:in `prepare_upsert'
      mongoid (6.1.0) lib/mongoid/persistable/upsertable.rb:23:in `upsert'
      ahoy_matey (1.5.5) lib/ahoy/stores/mongoid_store.rb:34:in `track_event'
      config/initializers/ahoy.rb:5:in `track_event'}}

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            CyrilDD Cyril
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: