使用 mongoimport 导入 1 个以上的 json 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11609647/
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-03 18:28:09  来源:igfitidea点击:

Import more than 1 json file using mongoimport

jsonmongodbmongoimport

提问by user850234

I am new to mongodband want to know about importing a jsonfile from one server to another. I tried the following command mongoimport -d test -c bik check.jsonand it works fine for me. Now i want to know when there are multiple jsonfiles how do i import all of them at a single go. I could not find any related document where it is written this is not possible. Please help me is this possible and how

我是新手mongodb,想知道如何将json文件从一台服务器导入到另一台服务器。我尝试了以下命令mongoimport -d test -c bik check.json,它对我来说很好用。现在我想知道当有多个json文件时如何一次性导入所有文件。我找不到任何相关文件,这是不可能的。请帮助我这是否可能以及如何

采纳答案by Sergio Tulentsev

You can always write some shell scripts.

您总是可以编写一些 shell 脚本。

colls=( mycoll1 mycoll2 mycoll5 )

for c in ${colls[@]}
do
  mongoimport -d mydb -c $c.json
done

回答by romaninsh

I came up with a more elegant way to automatically import ALL collections:

我想出了一种更优雅的方法来自动导入所有集合:

ls -1 *.json | sed 's/.json$//' | while read col; do 
    mongoimport -d db_name -c $col < $col.json; 
done

I hope this is helpful.

我希望这是有帮助的。

回答by Tomi Kokkonen

Windows Batch version:

Windows 批处理版本:

@echo off
for %%f in (*.json) do (
    "mongoimport.exe" --jsonArray --db databasename --collection collectioname --file %%~nf.json
)

回答by helpdoc

You can do it by this way also :

你也可以通过这种方式做到:

for filename in *; do mongoimport --db <Database> --collection <Collection Name> --file $filename; done

回答by Kanagavelu Sugumar

This worked for me in MAC OS X

这在 MAC OS X 中对我有用

find . -regex '.*/[^/]*.json' | xargs -L 1 mongoimport --db DB_NAME -u USER_NAME -p PASSWORD --collection COLLECTION_NAME  --file

回答by Eduardo

For windowsbat file. This would be way better if you have a list of json files in the folder. and the collection name matches the name in files

对于windowsbat 文件。如果文件夹中有 json 文件列表,这会更好。并且集合名称与文件中的名称匹配

@echo off
for %%f in (*.json) do (
    "mongoimport.exe" --db databasename --collection %%~nf --drop --file %%f
)
pause

回答by Sam Kenny

Another one line solution (assuming you are in the folder where the json files are):

另一个单行解决方案(假设您在 json 文件所在的文件夹中):

ls | sed 's/.json$//' | xargs -I{} mongoimport -d DATABASE_NAME -c {} {}.json

回答by Teeks

I used the solutions here to add a shell function to my bash profile for doing this quickly.

我使用这里的解决方案将 shell 函数添加到我的 bash 配置文件中以快速执行此操作。

My example depends on the mongo export outputting each collection as a file with the collection name and .metadata.jsonextension.

我的示例取决于 mongo 导出,将每个集合输出为具有集合名称和.metadata.json扩展名的文件。

function mimport() { for filename in *; do collection="${filename%.metadata.json}"; mongoimport --db $1 --collection $collection --file $filename; done }

function mimport() { for filename in *; do collection="${filename%.metadata.json}"; mongoimport --db $1 --collection $collection --file $filename; done }

Use in the path of the export files, passing the DB name to the command...

在导出文件的路径中使用,将数据库名称传递给命令...

mimport my_db

mimport my_db

Will load all collections into the DB at localhost.

将所有集合加载到本地主机的数据库中。

回答by Juan Miguel Díaz Pérez

Linux:

Linux:

> cat one.json two.json > three.json

> mongoimport --db foo --collection baz --file three.json"

Or, all files in the folder :

或者,文件夹中的所有文件:

> cat *.json > big.json

> mongoimport --db foo --collection baz --file "big.json"

回答by user854301

One line solution:

一行解决方案:

for /F %i in ('dir /b c:\files\*.json') do mongoimport.exe /d db /c files /file c:\file\%i

for /F %i in ('dir /b c:\files\*.json') do mongoimport.exe /d db /c files /file c:\file\%i