【问题标题】:Using DBRef in mongo ruby driver: (undefined method `bson_type' for #<Mongo::DBRef:0x0056466ed55e48>)在 mongo ruby​​ 驱动程序中使用 DBRef:(#<Mongodb::DBRef:0x0056466ed55e48> 的未定义方法 `bson type')
【发布时间】:2015-12-23 05:38:35
【问题描述】:

当我尝试在 mongo ruby​​ 驱动程序中使用 DBRef(创建一个新的 DBRef 对象并包含在我要插入到集合中的文档中)时,我收到了这个错误,我无法做出正面或反面:

NoMethodError (undefined method `bson_type' for #<Mongo::DBRef:0x0056466ed55e48>):
  app/controllers/payment_notifications_controller.rb:43:in `block in create'
  app/controllers/payment_notifications_controller.rb:19:in `create'

这里是有问题的代码:

user_mongo = Urgent::Application.config.mongo_client[:user].find(uuid: order.identity.uuid)
if user_mongo
  grant_document = { :target => Mongo::DBRef.new("user", user_mongo.first["_id"]), :role => order_item.expirable.backend_id, :created => Time.now, :store_item_id => order_item.id, :store_order_id => order.id }

  if expires
    grant_document[:expires] = expires
  end

  Urgent::Application.config.mongo_client[:grant].insert_one(grant_document)
end

第 39 行是代码 sn-p 中倒数第二行。

【问题讨论】:

    标签: ruby-on-rails ruby mongodb mongodb-ruby


    【解决方案1】:

    Ruby Mongo 驱动程序(我使用的是 v2.2.0)没有在 Mongo::DBRef 类上定义 bson_type 方法。我可以通过将数据库引用指定为扩展 JSON (link) 来解决这个特殊限制。将您的 grant_document 哈希定义为:

    grant_document = { :target => { "$ref" => "user", "$id" => user_mongo.first["_id"] }, :role => ... }
    

    【讨论】:

      猜你喜欢
      • 2020-04-02
      • 2012-02-01
      • 2015-10-21
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 2012-11-01
      • 1970-01-01
      相关资源
      最近更新 更多