MongoDB 的 BSON gem 无法读取 MongoDB 数据库文件?

发布于 2024-11-28 21:24:16 字数 869 浏览 1 评论 0原文

我有一个包含一些数据的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁客 2024-12-05 21:24:16

添加艾米丽的答案:您可以使用一些实用程序直接检查文件。首先,使用 mongodump 转储数据库:

mongodump

这会将数据文件转储到原始 BSON。

然后您可以使用 bsondump 检查 bson:

bsondump dump/test/foo.bson

还有一个随 gem 附带的 Ruby 实用程序:

b2json dump/test/foo.bson

Adding to Emily's answer: there are some utilities that you can use to examine the files directly. First, dump the database using mongodump:

mongodump

That will dump the data files to raw BSON.

Then you can examine the bson with bsondump:

bsondump dump/test/foo.bson

There's also a Ruby utility that ships with the gem:

b2json dump/test/foo.bson
留蓝 2024-12-05 21:24:16

MongoDB 不是基于文件的数据库。为了读回数据,您需要连接到正在运行的 MongoDB 服务器。 BSON gem 用于解析服务器返回的数据,而不是解析文件本身。

要连接到 MongoDB 服务器并读取数据,您将执行以下操作:

require 'rubygems'
require 'mongo'

connection = Mongo::Connection.new # connects to localhost by default
db   = connection['sample-db']
collection = db['test']

outfile = File.open('output.txt', 'w')
collection.find.each { |doc| outfile.puts doc.inspect }

collectiondb 对象具有其他方法,可让您获取所有数据的列表数据库和集合,如果您还需要打印所有这些中的数据。查看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:

require 'rubygems'
require 'mongo'

connection = Mongo::Connection.new # connects to localhost by default
db   = connection['sample-db']
collection = db['test']

outfile = File.open('output.txt', 'w')
collection.find.each { |doc| outfile.puts doc.inspect }

The collection and db 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文