mongodb Mongo DB 并将 bson 文件插入数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11817812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:46:59  来源:igfitidea点击:

Mongo DB and inserting bson files into a database

mongodb

提问by Tampa

I was given a data dump of bson files. In the mongo db, the database and the collections exists. These are updates to each of the collections in the database. So, in the given directory, there are about 30 bson files for each collection.

我得到了 bson 文件的数据转储。在 mongo db 中,存在数据库和集合。这些是对数据库中每个集合的更新。因此,在给定目录中,每个集合大约有 30 个 bson 文件。

From the command line, I am using ubuntu, how do I append and load? Mongo is on my localhost with no username or password.

在命令行中,我使用的是 ubuntu,如何追加和加载?Mongo 在我的本地主机上,没有用户名或密码。

Thanks

谢谢

回答by Engineer

Took me a while to get around this excuse for an error. In the end I went to the directory outside of my dumpfolder, and did the following...

我花了一段时间来解决这个错误的借口。最后,我转到dump文件夹外的目录,并执行以下操作...

For a full DB restore:

对于完整的数据库还原:

mongorestore --drop dump/mydb

mongorestore --drop dump/mydb

Note that the mongodumpoperation will create individual folders for each database withinthe dumpfolder it creates, so you need to specify the full relative path, as above.

请注意,该mongodump操作将dump其创建的文件夹中为每个数据库创建单独的文件夹,因此您需要指定完整的相对路径,如上所示。

For a single collection:

对于单个集合:

mongorestore --drop -d mydb -c mycollection dump/mydb/mycollection.bson

mongorestore --drop -d mydb -c mycollection dump/mydb/mycollection.bson

回答by zag2art

The usual syntax is:

通常的语法是:

mongorestore -d dbname -c collectionname dir/file.bson

回答by Anderson Lopes

  • import Bson

    • mongorestore -d dbname -c collectionname dir/file.bson
  • import Json

    • mongoimport --collection NAME --file NAME.
  • 导入 Bson

    • mongorestore -d dbname -c collectionname dir/file.bson
  • 导入 Json

    • mongoimport --collection NAME --file NAME。

http://docs.mongodb.org/v2.2/reference/mongoimport/

http://docs.mongodb.org/v2.2/reference/mongoimport/

回答by Sammaye

Since Mongo restore does not update the current records this would not be a good choice.

由于 Mongo restore 不会更新当前记录,因此这不是一个好的选择。

Mongorestore only appends new records as stated:

Mongorestore 仅附加新记录,如下所示:

mongorestore just does inserts with the data to restore; if existing data (like with the same _id) is there it will not be replaced.

mongorestore 只是插入要恢复的数据;如果现有数据(如具有相同的 _id)存在,则不会被替换。

You may wish to build a BSON parser in your language of choice and make a more complex tool than mongorestore, since mongorestore is only designed to "restore" (as the name kinda suggests) a database/collection you will need to write something a little more complicated to do what you want and that depends heavily on your server-side language.

您可能希望使用您选择的语言构建一个 BSON 解析器,并制作一个比 mongorestore 更复杂的工具,因为 mongorestore 仅旨在“恢复”(顾名思义)数据库/集合,您需要编写一些东西做你想做的事情更复杂,这在很大程度上取决于你的服务器端语言。

Edit

编辑

This is actually better done with mongoexport and mongoimport:

这实际上用 mongoexport 和 mongoimport 做得更好:

http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongoimport

http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongoimport

With mongoexport you could export a JSON file and give the command line for it do upserts. So I would personally go back to the person who gave this file and tell them that you actually want a mongo export file instead.

使用 mongoexport,您可以导出 JSON 文件并为其提供命令行来执行 upsert。所以我会亲自回到提供这个文件的人那里,告诉他们你实际上想要一个 mongo 导出文件。

回答by Paras Mathur

1) Go to the directory where the "dump" folder is located in CMD.

1)进入CMD中“dump”文件夹所在的目录。

2) Run the mongorestorecommand.

2) 运行mongorestore命令。