MongoDB 从文件夹中导入 bson 文件

sai*_*sai 5 mongodb mongodump mongorestore

我无法将.bson计算机上的( ) 文件导入MongoDB数据库。我一直在尝试以下命令:

1)mongorestore -d demo -c dcoll C:\data\dump\twitter\tweets.bson
Run Code Online (Sandbox Code Playgroud)

2)mongorestore -d demo -c dcoll /dump/twitter/tweets.bson
Run Code Online (Sandbox Code Playgroud)

Md *_*han 2

mongorestore -d demo -c dcoll C:\data\dump\twitter\tweets.bson

当我检查过你的 MongoDB 语法查询时,它似乎是正确的。根据您的脚本代码demodatabasedcollcollection,您想要恢复.bson 文件中的tweets.bson.bson ( ) 文件。dcoll collection

我想说的是,在恢复MongoDB中的(.bson)文件之前。您必须确保您的数据库名称集合名称应位于 MongoDB 中。Collection如果数据库中没有,那么首先Collection在数据库中创建它。

例如,首先连接并mongo shell显示数据库和集合。您要在 MOngoDB 中恢复它是否存在。

显示 MongoDB 中的数据库

例如,在我的环境中,我已经检查过这些数据库存在于MongoDB.

> show dbs
admin             0.000GB
blog              0.011GB
citibike          0.338GB
city              0.002GB
enron             0.213GB
local             0.000GB
ships             0.001GB
test              0.164GB
video             0.237GB
week6             0.006GB
Run Code Online (Sandbox Code Playgroud)

假设我想恢复测试.bson数据库中的 ( ) 文件。

> use test
switched to db test
>
Run Code Online (Sandbox Code Playgroud)

然后我将在test中创建collection 数据 database

> db.createCollection("data")
{ "ok" : 1 }   // ok : 1 means collection successfully created 
Run Code Online (Sandbox Code Playgroud)

还要从查询中确认该集合是否data在数据库中。test

> show collections
airline
amwaj
cars
collection
data
movies
stuff
>
Run Code Online (Sandbox Code Playgroud)

这里存在数据收集,然后最终将 (.bson) 恢复到data collection.

注意:确保您从当前mongo shell位置恢复 (.bson) 文件mongorestore.exe。基本上这个文件可以在MongoDB 服务器BIN文件夹中找到。在我这里是这样。C:\Program Files\MongoDB\Server\3.6\bin

所以,最后的mongorestore命令会是这样的

C:\Program Files\MongoDB\Server\3.6\bin>mongorestore -d test -c data C:\data\dump\100YWeatherSmall\data.bson
    2017-12-25T09:55:50.531+0300    checking for collection data in C:\data\dump\100YWeatherSmall\data.bson
    2017-12-25T09:55:50.561+0300    reading metadata for test.data from C:\data\dump\100YWeatherSmall\data.metadata.json
    2017-12-25T09:55:50.565+0300    restoring test.data from C:\data\dump\100YWeatherSmall\data.bson
    2017-12-25T09:55:52.662+0300    [##......................]  test.data  43.9MB/403MB  (10.9%)
    2017-12-25T09:55:55.565+0300    [#####...................]  test.data  87.6MB/403MB  (21.7%)
    2017-12-25T09:55:58.501+0300    [#########...............]  test.data  152MB/403MB  (37.6%)
    2017-12-25T09:56:01.501+0300    [#############...........]  test.data  230MB/403MB  (57.1%)
    2017-12-25T09:56:04.501+0300    [################........]  test.data  282MB/403MB  (70.0%)
    2017-12-25T09:56:07.501+0300    [######################..]  test.data  377MB/403MB  (93.6%)
    2017-12-25T09:56:09.349+0300    [########################]  test.data  403MB/403MB  (100.0%)
    2017-12-25T09:56:09.349+0300    no indexes to restore
    2017-12-25T09:56:09.350+0300    finished restoring test.data (250000 documents)
    2017-12-25T09:56:09.350+0300    done
Run Code Online (Sandbox Code Playgroud)

在上面的代码中我使用了

mongorestore用于恢复 (.bson) 文件的命令。

-d // 用于数据库
-c // 用于集合

C:\data\dump\100YWeatherSmall\data.bson // (.bson) 文件的位置。

我希望这会对您有所帮助。

为了进一步您的参考mongorestore