Collection là khái niệm của MongoDB, tương đương với khái niệm Table trong cơ sở dữ liệu quan hệ (Oracle, SQL Server, MySQL,. )
mongodump. Dùng để xuất tất cả dữ liệu của cơ sở dữ liệu sang các tệp (Đặt trong một thư mục), bao gồm một số tệp (bson, json)
mongostore. Được sử dụng để nhập tất cả dữ liệu vào cơ sở dữ liệu từ thư mục kết xuất (Sản phẩm của mongodump)
2- Nhập/Xuất nhờ thu
2. 1- mongoexport
# 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
2. 1. 1- mongoexport ra tệp json
Trong trường hợp xuất đơn giản, bạn không cần sử dụng nhiều tùy chọn trong lệnh mongoexport
# Export to json file # This is the simplest syntax. # The default output is json file so you do not need to specify the output file type mongoexport -d database_name -c collection_name -o outfile.json
Ví dụ, xuất một bộ sưu tập sang tệp JSON
cd C:\DevPrograms\MongoDB\bin
Xuất Bộ sưu tập Bộ phận trong cơ sở dữ liệu myfirstdb sang tệp json. C. /kiểm tra/khoa. json
Ghi chú. MongoDB phân biệt chữ hoa chữ thường
- Phòng
- Nhân viên
- Hàng tồn kho
- Danh mục sản phẩm
mongoexport -d myfirstdb -c Department -o C:/test/department.json
Kết quả
Xem tệp vừa được xuất
2. 1. 2- mongoexport sang tệp csv
# Export to csv file # This is the simplest syntax: # In the case csv file, you must provide the list of columns Collection (Required). # List of columns separated by commas and no spaces. # Must declare what type of output file (--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
khoa mở. csv với Microsoft Excel
2. 1. 3- mongoexport và các tùy chọn
- LÀM
2. 2- mongoimport
# Import from json file mongoimport -d database_name -c collection_name outfile.json # Import from csv file # --headerline: Using the first row of data as the column name of the Collection. mongoimport -d database_name -c collection_name --type csv --file locations.csv --headerline
2. 2. 1- mongoimport từ tệp json
Nhập dữ liệu từ tệp json vào Bộ sưu tập
cd C:\DevPrograms\MongoDB\bin
________số 8_______
Xem kết quả trên các công cụ trực quan RoboMongo
2. 2. 2- mongoimport từ tệp csv
Nhập dữ liệu từ tệp csv vào Bộ sưu tập
# Import from csv file # This is the simplest syntax: # --headerline: Using the first row of data as the column name of the Collection. mongoimport -d database_name -c collection_name --type csv --file locations.csv --headerline
# Export to json file # This is the simplest syntax. # The default output is json file so you do not need to specify the output file type mongoexport -d database_name -c collection_name -o outfile.json 0
# Export to json file # This is the simplest syntax. # The default output is json file so you do not need to specify the output file type mongoexport -d database_name -c collection_name -o outfile.json 1
Kết quả xem trên công cụ trực quan RoboMongo
2. 3- mongoexport/mongoimport và các tùy chọn
Trong trường hợp chung, bạn có các tùy chọn để nhập/xuất, xem trong bảng sau
Tùy chọn Ý nghĩaVí dụ--helptạo thông báo trợ giúp -v [ --verbose ] dài dòng hơn (bao gồm nhiều lần để chi tiết hơn e. g. -vvvvv) -h [ --host ] máy chủ argmongo để kết nối với ("trái, phải" cho các cặp)--cổng cổng máy chủ arg. (Cũng có thể sử dụng --host hostname. port) --ipv6bật hỗ trợ IPv6 (được tắt theo mặc định) -d [ --db ] argdatabase để sử dụng -c [ --collection ] argcollection để sử dụng (một số lệnh) -u [ --username ] argusername -p [ --password . g. -f name,age --fieldFile argfile với các tên trường - 1 trên mỗi dòng --ignoreBlanksif đã cho, các trường trống trong csv và tsv sẽ bị bỏ qua --nhập loại đối số của tệp để nhập. vỡ nợ. json (json,csv,tsv) --tệp argfile để nhập từ đó; . Bạn nên chắc chắn rằng điều này được lập chỉ mục. --stopOnErrorngừng nhập khi gặp lỗi đầu tiên thay vì tiếp tục --jsonArraytải một mảng json, không phải một mục trên mỗi dòng. Hiện tại giới hạn ở 4MB.
3- Cơ sở dữ liệu Nhập/Xuất
mongodump dùng để export toàn bộ cơ sở dữ liệu vào một thư mục
mongostore dùng để nhập tất cả dữ liệu vào cơ sở dữ liệu từ một thư mục (xuất sản phẩm của mongodump)
3. 1- mongodump
# Export to json file # This is the simplest syntax. # The default output is json file so you do not need to specify the output file type mongoexport -d database_name -c collection_name -o outfile.json 2
Thí dụ
Xuất cơ sở dữ liệu myfirstdb vào thư mục. C. /kiểm tra
cd C:\DevPrograms\MongoDB\bin
# Export to json file # This is the simplest syntax. # The default output is json file so you do not need to specify the output file type mongoexport -d database_name -c collection_name -o outfile.json 4
Kết quả, thư mục con myfirstdb được tạo trong thư mục C. /test, nó chứa một số tệp
3. 2- mongorestore
# Export to json file # This is the simplest syntax. # The default output is json file so you do not need to specify the output file type mongoexport -d database_name -c collection_name -o outfile.json 5
Ví dụ, thư mục C. /test/myfirstdb chứa các tệp kết xuất. Chúng tôi sẽ sử dụng nó để nhập vào cơ sở dữ liệu. mydb2