BSON::ObjectId vs Mongo::ObjectID

2019-07-07 03:08发布

问题:

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