我安装了tutorial
$gem install mongo
Successfully installed mongo-1.8.6
1 gem installed
Installing ri documentation for mongo-1.8.6...
Installing RDoc documentation for mongo-1.8.6...
和
gem install bson_ext
Building native extensions. This could take a while...
Successfully installed bson_ext-1.8.6
1 gem installed
Installing ri documentation for bson_ext-1.8.6...
Installing RDoc documentation for bson_ext-1.8.6...
我的mongo.rb
#!/usr/bin/ruby -w
require 'mongo'
include Mongo
mongo_client = MongoClient.new("localhost", 27017)
db = mongo_client.db("somedb")
coll = db.collection("somecoll")
当我运行mongo.rb时,我收到错误:
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/bson_ext-1.8.6/ext/bson_ext/bson_ext/cbson.so: warning: method redefined; discarding old legal?
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/bson-1.8.6/lib/bson/types/object_id.rb:39: warning: previous definition of legal? was here
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/bson_ext-1.8.6/ext/bson_ext/bson_ext/cbson.so: warning: method redefined; discarding old from_string
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/bson-1.8.6/lib/bson/types/object_id.rb:107: warning: previous definition of from_string was here
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/bson_ext-1.8.6/ext/bson_ext/bson_ext/cbson.so: warning: method redefined; discarding old to_s
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/bson-1.8.6/lib/bson/types/object_id.rb:119: warning: previous definition of to_s was here
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/mongo-1.8.6/lib/mongo/util/pool_manager.rb:267: warning: mismatched indentations at 'end' with 'def' at 259
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/mongo-1.8.6/lib/mongo/db.rb:339: warning: assigned but unused variable - e
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/mongo-1.8.6/lib/mongo/gridfs/grid.rb:24: warning: assigned but unused variable - connection
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/mongo-1.8.6/lib/mongo/gridfs/grid_file_system.rb:25: warning: assigned but unused variable - connection
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/mongo-1.8.6/lib/mongo/util/write_concern.rb:32: warning: instance variable @legacy_write_concern not initialized
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/mongo-1.8.6/lib/mongo/util/write_concern.rb:32: warning: instance variable @legacy_write_concern not initialized
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/mongo-1.8.6/lib/mongo/util/write_concern.rb:32: warning: instance variable @legacy_write_concern not initialized
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/mongo-1.8.6/lib/mongo/util/logging.rb:41: warning: instance variable @instrumenter not initialized
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/mongo-1.8.6/lib/mongo/mongo_client.rb:523: warning: instance variable @max_message_size not initialized
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/mongo-1.8.6/lib/mongo/mongo_client.rb:519: warning: instance variable @max_bson_size not initialized
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/mongo-1.8.6/lib/mongo/mongo_client.rb:519: warning: instance variable @max_bson_size not initialized
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/mongo-1.8.6/lib/mongo/util/write_concern.rb:32: warning: instance variable @legacy_write_concern not initialized
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/mongo-1.8.6/lib/mongo/util/write_concern.rb:32: warning: instance variable @legacy_write_concern not initialized
但是,在irb中似乎没问题:
$irb
1.9.3-p429 :001 > require 'mongo'
=> true
1.9.3-p429 :002 > include Mongo
=> Object
1.9.3-p429 :003 > mongo_client = MongoClient.new("localhost", 27017)
=> #<Mongo::MongoClient:0x00000001b5d618 @host="localhost", @port=27017, @id_lock=#<Mutex:0x00000001b5d550>, @primary=["localhost", 27017], @primary_pool=#<Mongo::Pool:0xdb22d4 @host=localhost @port=27017 @ping_time= 0/1 sockets available up=true>, @mongos=false, @tag_sets=[], @acceptable_latency=15, @slave_ok=nil, @ssl=nil, @unix=false, @socket_class=Mongo::TCPSocket, @auths=[], @pool_size=1, @pool_timeout=5.0, @op_timeout=nil, @connect_timeout=30, @logger=nil, @read=:primary, @write_concern={:w=>1, :j=>false, :fsync=>false, :wtimeout=>nil}, @read_primary=true, @max_bson_size=16777216, @max_message_size=48000000>
1.9.3-p429 :004 > db = mongo_client.db("somedb")
=> #<Mongo::DB:0x00000001b66e48 @name="somedb", @connection=#<Mongo::MongoClient:0x00000001b5d618 @host="localhost", @port=27017, @id_lock=#<Mutex:0x00000001b5d550>, @primary=["localhost", 27017], @primary_pool=#<Mongo::Pool:0xdb22d4 @host=localhost @port=27017 @ping_time= 0/1 sockets available up=true>, @mongos=false, @tag_sets=[], @acceptable_latency=15, @slave_ok=nil, @ssl=nil, @unix=false, @socket_class=Mongo::TCPSocket, @auths=[], @pool_size=1, @pool_timeout=5.0, @op_timeout=nil, @connect_timeout=30, @logger=nil, @read=:primary, @write_concern={:w=>1, :j=>false, :fsync=>false, :wtimeout=>nil}, @read_primary=true, @max_bson_size=16777216, @max_message_size=48000000>, @strict=nil, @pk_factory=nil, @write_concern={:w=>1, :j=>false, :fsync=>false, :wtimeout=>nil}, @read=:primary, @tag_sets=[], @acceptable_latency=15, @cache_time=300>
1.9.3-p429 :005 > coll = db.collection("somecoll")
=> #<Mongo::Collection:0x00000001ba9518 @name="somecoll", @db=#<Mongo::DB:0x00000001b66e48 @name="somedb", @connection=#<Mongo::MongoClient:0x00000001b5d618 @host="localhost", @port=27017, @id_lock=#<Mutex:0x00000001b5d550>, @primary=["localhost", 27017], @primary_pool=#<Mongo::Pool:0xdb22d4 @host=localhost @port=27017 @ping_time= 0/1 sockets available up=true>, @mongos=false, @tag_sets=[], @acceptable_latency=15, @slave_ok=nil, @ssl=nil, @unix=false, @socket_class=Mongo::TCPSocket, @auths=[], @pool_size=1, @pool_timeout=5.0, @op_timeout=nil, @connect_timeout=30, @logger=nil, @read=:primary, @write_concern={:w=>1, :j=>false, :fsync=>false, :wtimeout=>nil}, @read_primary=true, @max_bson_size=16777216, @max_message_size=48000000>, @strict=nil, @pk_factory=nil, @write_concern={:w=>1, :j=>false, :fsync=>false, :wtimeout=>nil}, @read=:primary, @tag_sets=[], @acceptable_latency=15, @cache_time=300>, @connection=#<Mongo::MongoClient:0x00000001b5d618 @host="localhost", @port=27017, @id_lock=#<Mutex:0x00000001b5d550>, @primary=["localhost", 27017], @primary_pool=#<Mongo::Pool:0xdb22d4 @host=localhost @port=27017 @ping_time= 0/1 sockets available up=true>, @mongos=false, @tag_sets=[], @acceptable_latency=15, @slave_ok=nil, @ssl=nil, @unix=false, @socket_class=Mongo::TCPSocket, @auths=[], @pool_size=1, @pool_timeout=5.0, @op_timeout=nil, @connect_timeout=30, @logger=nil, @read=:primary, @write_concern={:w=>1, :j=>false, :fsync=>false, :wtimeout=>nil}, @read_primary=true, @max_bson_size=16777216, @max_message_size=48000000>, @logger=nil, @cache_time=300, @cache={}, @write_concern={:w=>1, :j=>false, :fsync=>false, :wtimeout=>nil}, @read=:primary, @capped=nil, @tag_sets=[], @acceptable_latency=15, @pk_factory=BSON::ObjectId, @hint=nil>
1.9.3-p429 :006 >
实际上,当我运行mongo.rb时,它不应该显示任何内容,因为我只是指定要使用的数据库和集合。
MongoDB版本是2.4.3。
很抱歉,主题并不具体。
答案 0 :(得分:2)
这些不是错误,而是警告:
/home/askar/.rvm/gems/ruby-1.9.3-p429/gems/bson_ext-1.8.6/ext/bson_ext/bson_ext/cbson.so: 警告:方法已重新定义;抛弃旧法律?
它们不会阻止您执行程序,它们只会告诉您代码中的潜在问题:未初始化的实例变量,重写的方法等。
它们显示是因为您使用ruby
选项呼叫-w
。您可以对irb
使用相同的选项:
$ irb -w
irb> require 'mongodb'
/home/toro2k/.gem/ruby/1.9.1/gems/bson_ext-1.8.6/ext/bson_ext/bson_ext/cbson.so: warning: method redefined; discarding old legal?
...
...