1

我想将 bson_ext 与 mongodb 一起使用,但出现一个奇怪的错误。

我试图运行这个 ruby​​ 测试代码:

require 'mongo'
include Mongo
mongoClient = MongoClient.new("localhost", 27017)

错误是:

/home/user/.rvm/gems/ruby-2.0.0-p353/gems/bson-1.9.2/lib/bson/bson_c.rb:20:in serialize': BSON.serialize takes a Hash but got a Hash (BSON::InvalidDocument) from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/bson-1.9.2/lib/bson/bson_c.rb:20:inserialize' ...

我使用以下 Gemfile 运行 bundle install :

gem "bson"
gem "bson_ext"
gem "mongo"

(安装的版本都是1.9.2除了 ruby​​ 是 2.0.0)

我真的不知道该怎么办。谁能指出我正确的方向?

4

1 回答 1

1

I tried to reproduce this but it worked for me with ruby 2.0.0p247

$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [universal.x86_64-darwin13]

I installed the same versions of gems.

$ gem list

*** LOCAL GEMS ***

bson (1.9.2, 1.3.1)
bson_ext (1.9.2, 1.3.1)
bundler (1.5.1)
CFPropertyList (2.2.0)
libxml-ruby (2.6.0)
mongo (1.9.2, 1.3.1)
nokogiri (1.5.6)
sqlite3 (1.3.7)

Trying to connect

$ irb
irb(main):001:0> require 'mongo'
=> true
irb(main):002:0> include Mongo
=> Object
irb(main):003:0> mongoClient = MongoClient.new("localhost", 27017)
=> #<Mongo::MongoClient:0x007f971b96fd70 @host="localhost", @port=27017, @id_lock=#<Mutex:0x007f971b96fcd0>, @primary=["localhost", 27017], @primary_pool=#<Mongo::Pool:0x3fcb8dcb6e00 @host=localhost @port=27017 @ping_time= 0/1 sockets available up=true>, @mongos=false, @tag_sets=[], @acceptable_latency=15, @max_message_size=48000000, @max_bson_size=16777216, @slave_ok=nil, @ssl=nil, @unix=false, @socket_opts={}, @socket_class=Mongo::TCPSocket, @auths=[], @pool_size=1, @pool_timeout=5.0, @op_timeout=nil, @connect_timeout=30, @logger=nil, @read=:primary, @default_db="test", @write_concern={:w=>1, :j=>false, :fsync=>false, :wtimeout=>nil}, @read_primary=true>

Stopped the mongod to check its really connecting

irb(main):004:0> mongoClient = MongoClient.new("localhost", 27017)
Mongo::ConnectionFailure: Failed to connect to a master node at localhost:27017
from /Library/Ruby/Gems/2.0.0/gems/mongo-1.9.2/lib/mongo/mongo_client.rb:486:in `connect'
from /Library/Ruby/Gems/2.0.0/gems/mongo-1.9.2/lib/mongo/mongo_client.rb:693:in `setup'
from /Library/Ruby/Gems/2.0.0/gems/mongo-1.9.2/lib/mongo/mongo_client.rb:155:in `initialize'
from (irb):4:in `new'
from (irb):4
from /usr/bin/irb:12:in `<main>'

I hope this helps.

于 2014-01-10T13:52:29.687 回答