Import và Export cơ sở dữ liệu MongoDB
Xem thêm các chuyên mục:

Là một website được viết trên công nghệ web Flutter vì vậy hỗ trợ rất tốt cho người học, kể cả những người học khó tính nhất.
Hiện tại website đang tiếp tục được cập nhập nội dung cho phong phú và đầy đủ hơn. Mong các bạn nghé thăm và ủng hộ website mới của chúng tôi.


MongoDB cung cấp cho bạn 2 cách để import/export cơ sở dữ liệu:
- mongoexport/mongoimport
- mongodump/mongostore
mongoexport: Sử dụng để export (xuất khẩu) dữ liệu từ một Collection ra một file (json, csv,..)
mongoimport: Sử dụng để import (nhập khẩu) dữ liệu vào một Collection từ một file (json, csv,..)
mongoimport: Sử dụng để import (nhập khẩu) dữ liệu vào một Collection từ một file (json, csv,..)
Collection là khái niệm của MongoDB, nó tương đương với khái niệm Table trong cơ sở dữ liệu quan hệ (Oracle, SQL Server, MySQL,..).
mongodump: Sử dụng để export (xuất khẩu) toàn bộ dữ liệu của một database ra các file (Để vào trong một thư mục), bao gồm một số file (bson, json)
mongostore: Sử dụng để import (nhập khẩu) dữ liệu vào một database từ thư mục dump (Sản phẩm của mongodump nói trên)
mongostore: Sử dụng để import (nhập khẩu) dữ liệu vào một database từ thư mục dump (Sản phẩm của mongodump nói trên)
# Export to json
mongoexport -d database_name - c collection_name -o outfile.json
# Export to file csv
mongoexport --csv -o /tmp/people.csv -d school -c people -f firstName,lastName,telephone,email
Trong trường hợp export đơn giản, bạn không cần sử dụng nhiều option (tùy chọn) trong câu lệnh mongoexport:
# Export ra file json
# Đây là cú pháp đơn giản nhất.
# Mặc định file đầu ra là json vì vậy không cần chỉ rõ kiểu file đầu ra
mongoexport -d database_name -c collection_name -o outfile.json
Ví dụ export một collection ra file JSON.
cd C:\DevPrograms\MongoDB\bin

Export collection Department trong cơ sở dữ liệu myfirstdb ra file json: C:/test/department.json.
Chú ý: MongoDB phân biệt chữ hoa chữ thường vì vậy hãy chú ý khi đặt tên collection.Tốt nhất bạn nên thống nhất cách đặt tên collection với chữ cái đầu tiên viết hoa. Ví dụ:
- Department
- Employee
- Inventory_Item
- Product_Category
mongoexport -d myfirstdb -c Department -o C:/test/department.json

Kết quả:

Xem file mới được export ra:


# Export ra file csv
# Đây là cú pháp đơn giản nhất:
# Với trường hợp file csv bạn phải cung cấp cả danh sách các cột của Collection (Bắt buộc).
# Danh sách các cột cách nhau dấu phẩy và không có khoảng trắng.
# Phải khai báo rõ kiểu file đầu ra (--csv)
mongoexport -d database_name -c collection_name -f column_1,column_2,column_3 --csv -o outfile.csv
mongoexport -d myfirstdb -c Department -f dept_id,dept_no,dept_name,location,description --csv -o C:/test/department.csv



Mở department.csv bằng Microsoft Excel:

- TODO
# Import từ file json
mongoimport -d database_name -c collection_name outfile.json
# Import từ file csv
# --headerline: Thông báo rằng sẽ sử dụng dòng dữ liệu đầu tiên làm tên các cột của Collection.
mongoimport -d database_name -c collection_name --type csv --file locations.csv --headerline
Import dữ liệu từ một file json vào một Collection:
cd C:\DevPrograms\MongoDB\bin

# Import từ file json
mongoimport -d database_name -c collection_name outfile.json
# Import vào database myfirstdb
# dữ liệu Insert vào Collection: Department2
# Từ file C:/test/department.json
mongoimport -d myfirstdb -c Department2 C:/test/department.json


Đây là hình ảnh nhìn trên công cụ trực quan RoboMongo:

Import dữ liệu từ một file csv vào một Collection:
# Import từ file csv
# Cú pháp dạng đơn giản nhất:
# --headerline: Thông báo rằng sẽ sử dụng dòng dữ liệu đầu tiên làm tên các cột của Collection.
mongoimport -d database_name -c collection_name --type csv --file locations.csv --headerline

cd C:\DevPrograms\MongoDB\bin


# Import từ file vào database: myfirstdb
# Vào collection: Department3
# Vị trí file: C:/test/department.csv
# --headerline: Tham số nói rằng sẽ sử dụng dòng đầu tiên làm tên cột.
mongoimport -d myfirstdb -c Department3 --type csv --file C:/test/department.csv --headerline


Kết quả nhìn trên công cụ trực quan RoboMongo:

Trong trường hợp tổng quát bạn có các tùy chọn (option) để import/export liệt kê trong bảng dưới đây:
Option | Meaning | Example |
--help | produce help message | |
-v [ --verbose ] | be more verbose (include multiple times for more verbosity e.g. -vvvvv) | |
-h [ --host ] arg | mongo host to connect to ("left,right" for pairs) | |
--port arg | server port. (Can also use --host hostname:port) | |
--ipv6 | enable IPv6 support (disabled by default) | |
-d [ --db ] arg | database to use | |
-c [ --collection ] arg | collection to use (some commands) | |
-u [ --username ] arg | username | |
-p [ --password ] arg | password | |
--dbpath arg | directly access mongod data files in the given path,instead of connecting to a mongod instance - needs to lock the data directory, so cannot be used if a mongod is currently accessing the same path | |
--directoryperdb | if dbpath specified, each db is in a separate directory | |
-f [ --fields ] arg | comma seperated list of field names e.g. -f name,age | |
--fieldFile arg | file with fields names - 1 per line | |
--ignoreBlanks | if given, empty fields in csv and tsv will be ignored | |
--type arg | type of file to import. default: json (json,csv,tsv) | |
--file arg | file to import from; if not specified stdin is used | |
--drop | drop collection first | |
--headerline | CSV,TSV only - use first line as headers | |
--upsert | insert or update objects that already exist | |
--upsertFields arg | comma-separated fields for the query part of the upsert. You should make sure this is indexed. | |
--stopOnError | stop importing at the first error rather than continuing | |
--jsonArray | load a json array, not one item per line. Currently limited to 4MB. |
mongodump sử dụng để export (xuất khẩu) toàn bộ một cơ sở dữ liệu Mongo ra một thư mục:
mongostore sử dụng để import (nhập khẩu) toàn bộ dữ liệu từ một thư mục (sản phẩm của mongodump) vào một database.
mongostore sử dụng để import (nhập khẩu) toàn bộ dữ liệu từ một thư mục (sản phẩm của mongodump) vào một database.
# Cú pháp export toàn bộ database ra một thư mục (Gồm một số file)
mongodump -d database_name -o output_directory
Ví dụ:
Export toàn bộ cơ sở dữ liệu myfirstdb ra thư mục C:/test
Export toàn bộ cơ sở dữ liệu myfirstdb ra thư mục C:/test

cd C:\DevPrograms\MongoDB\bin

mongodump -d myfirstdb -o C:/test


Kết quả, thư mục con myfirstdb được tạo ra trong thư mục C:/test, nó chứa một số file.

# Cú pháp import toàn bộ một database ở dạng đơn giản nhất.
mongorestore -d database_name path_to_database
Ví dụ thư mục C:/test/myfirstdb chứa các file được dump ra trước đó. Chúng ta sẽ sử dụng nó để import vào cơ sở dữ liệu: mydb2

cd C:\DevPrograms\MongoDB\bin

mongorestore -d mydb2 C:\test\myfirstdb


Xem kết quả trên công cụ trực quan RoboMongo:
