mongodb에서 .bson 파일 형식을 가져 오는 방법


162

mongodump명령을 사용하여 서버에서 데이터베이스를 내보내고 덤프가 .bson파일에 저장 됩니다. mongorestore명령을 사용하여 로컬 서버에서 가져와야합니다 . 그러나 작동하지 않습니다. 올바른 mongorestore명령 은 무엇이며 다른 명령 은 무엇입니까 tools to restore db?

답변:


342

.bson 파일 을 가져 오는 것은 매우 간단 합니다.

mongorestore -d db_name -c collection_name /path/file.bson

단일 컬렉션의 경우에만 사용 하십시오.

mongorestore --drop -d db_name -c collection_name /path/file.bson

다음으로 내 보낸 전체 폴더를 복원합니다 mongodump.

mongorestore -d db_name /path/

4
mongoexport단일 컬렉션 만 내보내는 경우를 통해 내 보낸 bson 파일에도 유용합니다 . mongoimport"잘못된 문자"오류가 발생했습니다. 감사!
divillysausages

4
SyntaxError : 누락; 문장 전 @ (shell) : 1 : 16
151291

3
이 명령이 mongo 콘솔이 아닌 명령 프롬프트에서 실행되도록 추가 할 수 있습니다. 그것은 새로운 사용자를 도울 것입니다
Dreams

2
이 답변에 추가하십시오. bson 파일을 원격으로 복원하려는 경우 Mongorestore -h [host] -u [user] -p [pass] -d [database] -c [collection] [bson file]
Jpepper

1
tar.gz에서 extrackting 한 후 전체 폴더에서 작업 해 주셔서 감사합니다.
Braham Dev Yadav

63

mongorestore에 의해 덤프 된 bson 파일을 가져 오는 데 사용하는 도구 mongodump입니다.

로부터 문서 :

mongorestore는 mongodump에서 출력을 가져와 복원합니다.

예:

# On the server run dump, it will create 2 files per collection
# in ./dump directory:
# ./dump/my-collection.bson
# ./dump/my-collection.metadata.json
mongodump -h 127.0.0.1 -d my-db -c my-collection

# Locally, copy this structure and run restore.
# All collections from ./dump directory are picked up.
scp user@server:~/dump/**/* ./
mongorestore -h 127.0.0.1 -d my-db

dump/dbName/collectionName.bson폴더 구조 를 유지해야 합니까? 을 사용 mongodump했지만 이제는 mongorestore원격 Linux 상자에서 사용하고 싶습니다 .
케빈 메러디스

@Kevin : 솔직히 확신하지는 않지만 그렇게 가정합니다. FTP 프로그램을 사용하여 파일을 폴더 구조와 함께 전송 하시겠습니까?
Cameron

5
"dump"가 아닌 폴더 이름을 사용하여 mongo db를 성공적으로 가져올 수있었습니다.mongorestore --collection people --db accounts myDump/accounts/people.bson
Kevin Meredith

35
bsondump collection.bson > collection.json

그리고

mongoimport -d <dbname> -c <collection> < collection.json

7
MongoDB의 수출입 문서 에서 경고에 주목하십시오 :mongoimport and mongoexport do not reliably preserve all rich BSON data types because JSON can only represent a subset of the types supported by BSON. As a result, data exported or imported with these tools may lose some measure of fidelity.
LucasB

네. 중국어 / 일본어 문자가 엉망입니다.
user2619824

10

명령 행에서 다음을 실행하면 Mongo bin 디렉토리에 있어야합니다.

mongorestore -d db_name -c collection_name path/file.bson


9

Mongo Shell이 ​​아닌 cmd를 통해이 mongorestore 명령을 실행해야합니다 ... 아래 명령을 살펴보십시오 ...

Mongo 쉘이 아닌 cmd 에서이 명령을 실행하십시오.

>path\to\mongorestore.exe -d dbname -c collection_name path\to\same\collection.bson

mongodb의 bin 폴더 내부 path\to\mongorestore.exe경로는 다음과 같습니다 mongorestore.exe. dbname은 데이터베이스 이름입니다. collection_namecollection.bson의 이름입니다. path\to\same\collection.bson해당 컬렉션까지의 경로입니다.

이제 mongo shell에서 데이터베이스가 생성되었는지 여부를 확인할 수 있습니다 (존재하지 않으면 동일한 이름의 데이터베이스가 콜렉션으로 작성 됨).



2

원격 액세스가 가능하다면

bson의 경우 :

mongorestore --host m2.mongodb.net --port 27016 --ssl --username $user --password $password --authenticationDatabase $authdb -d test -c people "/home/${USER}/people.bson"

.gz (gzip) 형식으로 압축 된 bson의 경우 :

mongorestore --host m2.mongodb.net --port 27016 --ssl --username $user --password $password --authenticationDatabase $authdb -d test -c people --gzip --dir "/home/${USER}/people.bson.gz"

1

mongorestore -d db_name / path /

mongoDb의 bin 폴더에서이 쿼리를 실행하십시오.

C : \ Program Files \ MongoDB \ Server \ 4.2 \ bin-

그런 다음 위의 명령을 실행하십시오.


0

나는 이것을 사용했다 :

mongorestore -d databasename -c file.bson fullpath/file.bson

1. 속성에서 파일 경로와 파일 이름을 복사하십시오 (모든 bson 파일을 다른 폴더에 저장하십시오).

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.