2012-06-06 54 views
0

足够简单的情况。我有一个MongoDB数据库,其中包含以前开发人员提供的大量信息。然而,我对手前的模型的信息有限,而且我不能访问原始模型类。我一直在修改MongoDB驱动程序以获得更多的信息(最终必须使用MongoID来将对象映射回去)如下。使用Mongoid从mongoDB获取对象

#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,它方便调试恕我直言。

回答

2

所以这是一个破产。

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

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