충분히 간단한 상황. 이전 개발자의 정보가 담긴 MongoDB 데이터베이스가 있습니다. 그러나 필자는 이전에 제공된 모델에 대한 정보가 제한되어 있으며 DONT는 원래 모델 클래스에 액세스 할 수 없습니다. 나는 MongoDB 드라이버에 대해 좀 더 많은 정보를 얻기 위해 노력했다. (MongoID는 결국 객체를 다시 매핑해야한다.) 다음과 같이.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를 알아 내려고 위의 코드를 자유롭게 사용하면 IMHO 디버깅에 편리합니다.