BSON::ObjectId vs Mongo::ObjectID

杀马特。学长 韩版系。学妹 提交于 2019-12-12 15:22:26

问题


In online API they are referring to Mongo::ObjectID.

I have require 'mongo' but still ruby fails to find it. For instance, I need to find an object by its Id and I'm doing:

mongo_db['post'].find({_id: Mongo::ObjectID(params[:id])}).next

and it seems that it can't find Mongo::ObjectID and results in: NoMethodError - undefined method ``ObjectID' for Mongo:Module:

So after some time I started to require 'bson' and doing

mongo_db['post'].find({_id: BSON::ObjectId(params[:id])}).next

and it worked!

So what's the difference between BSON::ObjectId and Mongo::ObjectID and what do I need to do to make the latter one work (and do I really need to)?

PS: I've got

bson (1.5.2, 1.4.1)
    Authors: Jim Menard, Mike Dirolf, Kyle Banker
    Homepage: http://www.mongodb.org
    Installed at (1.5.2): /usr/local/lib64/ruby/gems/1.9.1
                 (1.4.1): /usr/local/lib64/ruby/gems/1.9.1

    Ruby implementation of BSON

mongo (1.5.2, 1.4.1)
    Authors: Jim Menard, Mike Dirolf, Kyle Banker
    Homepage: http://www.mongodb.org
    Installed at (1.5.2): /usr/local/lib64/ruby/gems/1.9.1
                 (1.4.1): /usr/local/lib64/ruby/gems/1.9.1

    Ruby driver for the MongoDB

回答1:


There are two changes to the Ruby driver that are relevant here. The first is:

https://jira.mongodb.org/browse/RUBY-158

That moved the ObjectID to the "camel case" version ObjectId. The switch from the Mongo namespace to the BSON namespace happened far earlier (0.2), see the last entry in the History file here:

https://github.com/mongodb/mongo-ruby-driver/blob/master/docs/HISTORY.md

The real problem, of course, is old and crufty documentation, so I have submitted a pull request to update it:

https://github.com/mongodb/mongo-ruby-driver/pull/90



来源:https://stackoverflow.com/questions/9386533/bsonobjectid-vs-mongoobjectid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!