MongoDB 的 BSON gem 无法读取 MongoDB 数据库文件?
我有一个包含一些数据的 MongoDB 数据库。一切正常,数据已正确插入到 mongo DB 中。不过,我现在想做的是打开 mongoDB DB 文件,并使用 BSON gem 解析它,以便我可以查看 mongo DB 文件的人类友好格式。
data = nil
File.open("input/bson/database_development.0") do |f|
data = f.read
end
unpacked_data = BSON.deserialize(data)
File.new("input/bson/output.txt", "w") do |f|
f.write(unpacked_data)
end
然而,这给了我以下错误:
/home/user/.rvm/gems/ruby-1.9.2-p180/gems/bson-1.3.1/lib/bson/bson_c.rb:28:in `deserialize': no c decoder for this type yet (-86) (TypeError)
from /home/user/.rvm/gems/ruby-1.9.2-p180/gems/bson-1.3.1/lib/bson/bson_c.rb:28:in `deserialize'
from /home/user/.rvm/gems/ruby-1.9.2-p180/gems/bson-1.3.1/lib/bson.rb:37:in `deserialize'
from bsoner.rb:16:in `<main>'
从谷歌一点,有人说 mongoDB 可以接受任何类型的输入并存储它,但不能读取任何类型的数据。所以他们说数据库文件中存在无法正确读取的错误数据。但它不应该能够读取它可以插入的任何内容吗?
I have a MongoDB DB with some data. That all works fine, the data was inserted into the mongo DB properly. What I want to do now, though, is open the mongoDB DB file, and parse it using the BSON gem so that I can look at the human-friendly format of the mongo DB file.
data = nil
File.open("input/bson/database_development.0") do |f|
data = f.read
end
unpacked_data = BSON.deserialize(data)
File.new("input/bson/output.txt", "w") do |f|
f.write(unpacked_data)
end
However, this gives me the following error:
/home/user/.rvm/gems/ruby-1.9.2-p180/gems/bson-1.3.1/lib/bson/bson_c.rb:28:in `deserialize': no c decoder for this type yet (-86) (TypeError)
from /home/user/.rvm/gems/ruby-1.9.2-p180/gems/bson-1.3.1/lib/bson/bson_c.rb:28:in `deserialize'
from /home/user/.rvm/gems/ruby-1.9.2-p180/gems/bson-1.3.1/lib/bson.rb:37:in `deserialize'
from bsoner.rb:16:in `<main>'
From google a little bit, some folks said that mongoDB can ACCEPT any kind of input and store it, but can't READ just any kind of data. So they're saying that the database file has bad data in it that can't be read properly. But shouldn't it be able to read anything that it can insert?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加艾米丽的答案:您可以使用一些实用程序直接检查文件。首先,使用 mongodump 转储数据库:
这会将数据文件转储到原始 BSON。
然后您可以使用 bsondump 检查 bson:
还有一个随 gem 附带的 Ruby 实用程序:
Adding to Emily's answer: there are some utilities that you can use to examine the files directly. First, dump the database using mongodump:
That will dump the data files to raw BSON.
Then you can examine the bson with bsondump:
There's also a Ruby utility that ships with the gem:
MongoDB 不是基于文件的数据库。为了读回数据,您需要连接到正在运行的 MongoDB 服务器。 BSON gem 用于解析服务器返回的数据,而不是解析文件本身。
要连接到 MongoDB 服务器并读取数据,您将执行以下操作:
collection
和db
对象具有其他方法,可让您获取所有数据的列表数据库和集合,如果您还需要打印所有这些中的数据。查看mongo gem 的 API 文档了解更多信息。MongoDB is not a file-based database. In order to read the data back out, you need to connect to the running MongoDB server. The BSON gem is for parsing the data returned by the server, not parsing the files themselves.
To connect to your MongoDB server and read data, you'll do something like the following:
The
collection
anddb
objects have additional methods that will let you get a list of all databases and collections, if you need to print out the data in all of those as well. Check out the API documentation for the mongo gem for more information.