我正在尝试使用MongoDB Ruby驱动程序通过mongoS从集群中提取信息。这就是我所做的。
@mongo_client = Mongo::Connection.new('mongoshost', 27320)
@db = @mongo_client.db("thedatabase")
@auth = @db.authenticate("username", "password")
if(@mongo_client)
print "Successfully connected to mongos\n"
else
print "Connection failed"
end
if(@auth == true)
print "Auth successful\n"
else
print "Auth failed"
end
collection = @db.collection("thecollection")
puts collection.find.to_a
当我运行它时,一切都会工作,直到它试图打印集合中的文档为止。这是我的输出:
C:\Code\whatever>ruby getdata.rb
Successfully connected to mongos
Auth successful
C:/Ruby200/lib/ruby/gems/2.0.0/gems/bson-1.9.2/lib/bson/bson_c.rb:24:in `deseria
lize': time must be positive (ArgumentError)
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/bson-1.9.2/lib/bson/bson_c.rb:2
4:in `deserialize'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-1.9.2/lib/mongo/networkin
g.rb:223:in `read_documents'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-1.9.2/lib/mongo/networkin
g.rb:169:in `receive'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-1.9.2/lib/mongo/networkin
g.rb:133:in `receive_message'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-1.9.2/lib/mongo/cursor.rb
:497:in `block in send_initial_query'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-1.9.2/lib/mongo/util/logg
ing.rb:55:in `block in instrument'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-1.9.2/lib/mongo/util/logg
ing.rb:20:in `instrument'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-1.9.2/lib/mongo/util/logg
ing.rb:54:in `instrument'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-1.9.2/lib/mongo/cursor.rb
:493:in `send_initial_query'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-1.9.2/lib/mongo/cursor.rb
:478:in `refresh'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-1.9.2/lib/mongo/cursor.rb
:124:in `next'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-1.9.2/lib/mongo/cursor.rb
:290:in `each'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-1.9.2/lib/mongo/cursor.rb
:314:in `to_a'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-1.9.2/lib/mongo/cursor.rb
:314:in `to_a'
from getdata.rb:27:in `<main>'
“时间必须是积极的”错误有一些搜索结果,但没有任何帮助我弄清楚问题是什么。这些文档中的一个字段确实为日期保留了一个负数,但我不确定为什么这是一个问题,因为它正在阅读它而不是写(例如,其中一个字段看起来像这样:{“md”:日期(-62135596800000)})
这是驱动程序的问题,我的代码是坏的,还是我需要以另一种方式解决这个问题?我对红宝石很新,所以任何帮助都会受到赞赏!
答案 0 :(得分:0)
您的数据很糟糕,正如您已经注意到的那样。问题是驱动程序试图将日期值(实际上只是BSON时间戳字段内的mongo数字)扩展为实际的DateTime对象。
当它在读取时尝试执行此操作时,数字无效且通胀失败。如果您修复了日期值,问题将得到纠正。
注意:如果在shell中执行此操作,请使用ISOate帮助程序输入正确的值。如果使用代码,则使用DateTime对象。不要使用字符串,因为你会进一步搞乱你的数据。