【问题标题】:Getting an object from mongoDB with Mongoid使用 Mongoid 从 mongoDB 获取对象
【发布时间】:2012-06-10 08:12:30
【问题描述】:

足够简单的情况。我有一个 MongoDB 数据库,其中包含来自以前开发人员的大量信息。但是,我对之前出现的模型的信息有限,并且我无权访问原始模型类。我一直在修改 MongoDB 驱动程序以获取有关它的更多信息(最终必须使用 MongoID 将对象映射回来),如下所示。

#The flow is as follows
  #Connection
  #Databases
  #Database
  #Collection
  #Hash Info

  #Setup the connection. you can supply attributes in the form of ("db",portno) but most of the time it will pick up the defaults
  conn = Mongo::Connection.new

  #Database info
  mongodbinfo =conn.database_names
  conn.database_info.each { |info| puts info.inspect }


  db = conn.db("db_name_here")
  db.collection_names.each { |collection| puts collection.inspect  }


  collection = db.collection("model_name_here")
  puts collection.inspect

  collection.find.each { |row|
    puts row.inspect
    puts row.class
  }

每一行都是一个单独的对象,并且在 MongoDB 工作时,每个对象/文档都是一个 BSON 对象。

所以底线问题是如何使用 mongoID 将 BSON 反序列化为模型?

P.s 如果您试图找出一个新的 mongoDB,请随意使用上面的代码,它对于调试恕我直言很方便。

【问题讨论】:

    标签: ruby mongodb orm sinatra mongoid


    【解决方案1】:

    所以这是一个半身像。

    最后,我使用 Mondb 驱动程序通过查询手动提取数据。然而,创建对象要困难得多。

    使用 ORM 时最好有实际模型。

    【讨论】:

      猜你喜欢
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多