Nhập MongoDB

Sử dụng lệnh

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
2 để nhập dữ liệu vào bộ sưu tập. Bạn nên cài đặt các công cụ cơ sở dữ liệu MongoDB để sử dụng lệnh
test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
2

Để cài đặt các công cụ cơ sở dữ liệu, hãy truy cập Công cụ cơ sở dữ liệu và tải xuống tệp zip cho nền tảng của bạn

Nhập MongoDB

Bây giờ, giải nén và sao chép tất cả. exe và dán chúng vào thư mục bin MongoDB. Trên Windows, đó là thư mục

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
4

Nhập MongoDB

Bây giờ, hãy mở thiết bị đầu cuối hoặc dấu nhắc lệnh và điều hướng đến vị trí bạn có tệp JSON để nhập để bạn không cần chỉ định toàn bộ đường dẫn

Sau đây là lệnh mongoimport

mongoimport --db database_name --collection collection_name ^
            --authenticationDatabase admin --username  --password  ^
            --file file_path 
    

Bây giờ, hãy thực hiện lệnh sau để nhập dữ liệu từ tệp

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
5 vào bộ sưu tập
test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
6

D. \MyData> mongoimport --db test --collection nhân viên --file nhân viên dữ liệu. json --jsonArray

Lệnh trên sẽ nhập dữ liệu vào bộ sưu tập

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
6 trong cơ sở dữ liệu
test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
8. Lưu ý rằng
test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
9 chỉ ra rằng dữ liệu trong tệp chứa trong một mảng

Nhập dữ liệu từ tệp CSV

Giả sử rằng bạn có tệp

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
0 mà bạn muốn nhập vào bộ sưu tập
test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
1 mới. Thực hiện lệnh sau để nhập dữ liệu từ tệp CSV

D. \MyData> mongoimport --db test --collection nhân viêndata --type csv --file nhân viên. csv --fields _id,firstName,lastName

Tùy chọn

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
2 cho biết tên trường sẽ được sử dụng cho từng cột trong tệp CSV. Nếu một tệp chứa hàng tiêu đề sẽ được sử dụng làm tên trường thì hãy sử dụng tùy chọn
test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
3 thay vì
test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
2. Lệnh trên sẽ chèn tất cả dữ liệu vào bộ sưu tập
test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
6, như hình bên dưới

Có hai lệnh bạn cần sử dụng để sao lưu cơ sở dữ liệu MongoDB

  • mongodump
  • mongorestore

vòng đời dự phòng

  1. Tạo một bản sao của cơ sở dữ liệu (xuất) làm bản sao lưu
  2. Lấy nó từ máy chủ
  3. Đặt kho lưu trữ trở lại máy chủ
  4. Áp dụng (nhập) sao lưu mục tiêu

Xuất cơ sở dữ liệu

Xuất db

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
1 sang thư mục
test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
2

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
3

Nhập cơ sở dữ liệu

Thêm phím thả nếu có thể có xung đột phím

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
4

Tải xuống bản sao lưu từ máy chủ từ xa

Tạo kho lưu trữ với thư mục

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
2

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
6

Sau đó sao chép kho lưu trữ này vào máy tính của bạn thông qua tiện ích

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
7

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
8

Upload backup lên server của docker container

Tải nó lên máy chủ qua

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
7

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
0

Hoặc đặt nó vào bộ chứa docker cơ sở dữ liệu của bạn

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
20

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
21

Giải nén tệp lưu trữ trên máy chủ của bạn (vùng chứa)

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
22

Nếu bạn thấy một lỗi

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
23

Sau đó cài đặt gói

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
24

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
25

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

________số 8

Nhập MongoDB

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
  • Người lao động
  • Hàng tồn kho
  • Danh mục sản phẩm



mongoexport -d myfirstdb -c Department -o C:/test/department.json
 

Nhập MongoDB

Kết quả

Nhập MongoDB

Xem tệp vừa được xuất

Nhập MongoDB

Nhập MongoDB

2. 1. 2- mongoexport sang tệp csv

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
0

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
1

Nhập MongoDB

Nhập MongoDB

Nhập MongoDB

khoa mở. csv với Microsoft Excel

Nhập MongoDB

2. 1. 3- mongoexport và các tùy chọn

  • LÀM

2. 2- mongoimport

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
2

2. 2. 1- mongoimport từ tệp json

Nhập dữ liệu từ tệp json vào Bộ sưu tập

________số 8

Nhập MongoDB

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
4

Nhập MongoDB

Nhập MongoDB

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

Nhập MongoDB

2. 2. 2- mongoimport từ tệp csv

Nhập dữ liệu từ tệp csv vào Bộ sưu tập

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
5

Nhập MongoDB


# 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

Nhập MongoDB

Nhập MongoDB


# 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

Nhập MongoDB

Nhập MongoDB

Kết quả xem trên công cụ trực quan RoboMongo

Nhập MongoDB

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. mặc định. 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 ở 4 MB.  

3- Cơ sở dữ liệu Nhập/Xuất

mongodump dùng để xuất toàn bộ cơ sở dữ liệu sang 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

Ví dụ.
Xuất cơ sở dữ liệu myfirstdb vào thư mục. C. /kiểm tra
 

Nhập MongoDB

________số 8

Nhập MongoDB


# 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

Nhập MongoDB

Nhập MongoDB

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

Nhập MongoDB

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

Tôi có thể nhập dữ liệu vào MongoDB không?

MongoDB Compass có thể nhập dữ liệu vào bộ sưu tập từ tệp JSON hoặc CSV .

Làm cách nào để nhập MongoDB trong Node js?

Làm cách nào để khởi động MongoDB trong Node. js? .
Cài đặt trình điều khiển MongoDB, sử dụng npm
Nhập MongoClient ở đầu tệp của bạn
Tạo một phiên bản MongoClient mới và chuyển chuỗi kết nối của bạn làm tham số
Kết nối bằng cách sử dụng phương pháp kết nối. Để biết thêm chi tiết, hãy làm theo hướng dẫn này

Làm cách nào để nhập CSV MongoDB?

Nếu bạn có tệp CSV (hoặc tệp TSV - chúng giống nhau về mặt khái niệm) để nhập, hãy sử dụng tùy chọn --type=csv hoặc --type=tsv để thông báo . Ngoài ra, điều quan trọng là phải biết liệu tệp CSV của bạn có hàng tiêu đề hay không - trong đó dòng đầu tiên không chứa dữ liệu - thay vào đó nó chứa tên cho từng cột. . Also important is to know whether your CSV file has a header row - where the first line doesn't contain data - instead it contains the name for each column.

Làm cách nào để nhập tệp SQL trong MongoDB?

Nhấp vào Di chuyển SQL > Di chuyển SQL sang MongoDB trên thanh công cụ. .
2 – Chọn kết nối SQL nguồn. Bấm vào Bấm vào đây để kết nối với máy chủ để mở Trình quản lý kết nối SQL. .
3 – Chọn kết nối MongoDB mục tiêu của bạn. .
4 – Chọn bảng nguồn SQL. .
5 – Chạy nhập SQL