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 Show
Để 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 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 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 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' } ]6D. \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 CSVGiả 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 CSVD. \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
vòng đời dự phòng
Xuất cơ sở dữ liệuXuấ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ệuThê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ừ xaTạ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 containerTả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) 2- Nhập/Xuất nhờ thu2. 1- mongoexport
2. 1. 1- mongoexport ra tệp jsonTrong 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
Ví dụ, xuất một bộ sưu tập sang tệp JSON ________số 8 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
Kết quả Xem tệp vừa được xuất 2. 1. 2- mongoexport sang tệp csvtest> 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 khoa mở. csv với Microsoft Excel 2. 1. 3- mongoexport và các tùy chọn
2. 2- mongoimporttest> 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 jsonNhập dữ liệu từ tệp json vào Bộ sưu tập ________số 8 test> db.employees.find() [ { _id: 2, firstName: 'bill', lastName: 'gates' }, { _id: 1, firstName: 'steve', lastName: 'jobs' }, { _id: 3, firstName: 'james', lastName: 'bond' } ]4 Xem kết quả trên các công cụ trực quan RoboMongo 2. 2. 2- mongoimport từ tệp csvNhậ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 0 1Kết quả xem trên công cụ trực quan RoboMongo 2. 3- mongoexport/mongoimport và các tùy chọnTrong 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ấtmongodump dùng để xuất toàn bộ cơ sở dữ liệu sang một thư mục. 3. 1- mongodump 2Ví dụ. ________số 8 4Kế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 5Ví 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 |