Làm cách nào để nhập tệp vào 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' }
]
0 để 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' }
]
0

Để 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

Làm cách nào để nhập tệp vào 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' }
]
2

Làm cách nào để nhập tệp vào 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' }
]
3 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' }
]
4

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' }
]
4 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' }
]
6. 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' }
]
7 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' }
]
4, như hình bên dưới

MongoDB là một chương trình cơ sở dữ liệu định hướng tài liệu đa nền tảng nguồn có sẵn để lưu trữ khối lượng lớn. Được phân loại là chương trình cơ sở dữ liệu NoSQL, MongoDB sử dụng các tài liệu giống như JSON với các lược đồ tùy chọn


Để nhập cơ sở dữ liệu bằng công cụ MongoDB Cơ sở dữ liệu được quản lý DigitalOcean, bạn sẽ cần

  • Một ứng dụng khách MongoDB, như Mongo Shell hoặc mongoimport là một phần của Công cụ cơ sở dữ liệu MongoDB

  • Tệp cơ sở dữ liệu được lưu trữ cục bộ trên cùng một máy với ứng dụng khách MongoDB của bạn. Nếu bạn đang thử nghiệm với MongoDB, bạn có thể tải xuống tệp cơ sở dữ liệu mẫu từ trang web chính thức của MongoDB. Nếu không, hãy xem cách xuất dữ liệu từ cơ sở dữ liệu của bạn

  • Chi tiết kết nối của cơ sở dữ liệu. Để nhận các tham số kết nối của cơ sở dữ liệu từ bảng điều khiển của bạn, hãy truy cập trang Cơ sở dữ liệu cho cơ sở dữ liệu của bạn. Trên tab Tổng quan, bảng Chi tiết Kết nối có chuỗi Kết nối của bạn

Làm cách nào để nhập tệp vào mongodb?

Nhập dữ liệu

Để nhập dữ liệu vào cơ sở dữ liệu MongoDB, bạn có thể sử dụng

mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
2 để nhập dữ liệu bộ sưu tập cụ thể hoặc bạn có thể sử dụng
mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
3 để nhập bản sao lưu cơ sở dữ liệu đầy đủ nhị phân (BSON). Tệp cơ sở dữ liệu đã xuất phải được lưu trữ cục bộ trên cùng một máy với máy khách của bạn

Nhập với mongoimport

Lệnh

mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
2 nhập nội dung từ xuất JSON, CSV hoặc TSV vào cơ sở dữ liệu của bạn. Nó yêu cầu các giá trị cho các cờ sau

  • mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
    
    5. Chuỗi kết nối của cụm với cơ sở dữ liệu đích
  • mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
    
    6. Tên của bộ sưu tập mới bạn đang tạo

Bạn có thể tìm thấy chuỗi kết nối của cụm bằng cách tham khảo chi tiết kết nối của cụm của bạn

Lệnh

mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
2 sử dụng cú pháp sau

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

Một lệnh ví dụ trông như thế này

mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json

Trả về nhập thành công

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

Khi bạn đã nhập tệp cơ sở dữ liệu, bạn có thể xác minh tệp đã nhập bằng cách kết nối với cụm và chạy lệnh sau từ trình bao MongoDB

Lệnh trả về một danh sách tên bộ sưu tập

Nhập với mongorestore

Lệnh

mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
2 nhập nội dung từ xuất JSON, CSV hoặc TSV vào cơ sở dữ liệu của bạn. Nó yêu cầu các giá trị cho các cờ sau

  • mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
    
    5. Chuỗi kết nối của cụm với cơ sở dữ liệu đích

Bạn có thể tìm thấy chuỗi kết nối của cụm bằng cách tham khảo chi tiết kết nối của cụm của bạn

Lệnh

mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
2 sử dụng cú pháp sau

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

Một lệnh ví dụ trông như thế này

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

Trả về nhập thành công

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

Khi bạn đã nhập tệp cơ sở dữ liệu, bạn có thể xác minh tệp đã nhập bằng cách kết nối với cụm và chạy lệnh sau từ trình bao MongoDB

Lệnh trả về một danh sách tên bộ sưu tập

Xuất dữ liệu

Để xuất dữ liệu từ cơ sở dữ liệu MongoDB, bạn có thể sử dụng

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
41 để xuất dữ liệu bộ sưu tập cụ thể hoặc bạn có thể sử dụng
test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
42 để xuất bản sao lưu cơ sở dữ liệu đầy đủ nhị phân (BSON)

Xuất khẩu với mongoexport

Lệnh

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
41 tạo tệp xuất JSON, CSV hoặc TSV từ cơ sở dữ liệu của bạn. Nó yêu cầu các giá trị cho các cờ sau

  • mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
    
    5. Chuỗi kết nối của cụm với cơ sở dữ liệu đích
  • mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
    
    6. Tên của bộ sưu tập mới bạn đang tạo
  • test> db.employees.find()
    [
      { _id: 2, firstName: 'bill', lastName: 'gates' },
      { _id: 1, firstName: 'steve', lastName: 'jobs' },
      { _id: 3, firstName: 'james', lastName: 'bond' }
    ]
    
    46. Định dạng tệp xuất (JSON, CSV hoặc TSV)

Bạn có thể tìm thấy chuỗi kết nối của cụm bằng cách tham khảo chi tiết kết nối của cụm của bạn

Lệnh

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
41 sử dụng cú pháp sau

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

Một lệnh ví dụ trông như thế này

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

Xuất trả hàng thành công

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

Xuất khẩu với mongodump

Lệnh

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
42 tạo bản sao lưu đầy đủ nhị phân (BSON) cho cơ sở dữ liệu của bạn. Nó yêu cầu các giá trị cho các cờ sau

  • mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
    
    5. Chuỗi kết nối của cụm với cơ sở dữ liệu đích
  • test> db.employees.find()
    [
      { _id: 2, firstName: 'bill', lastName: 'gates' },
      { _id: 1, firstName: 'steve', lastName: 'jobs' },
      { _id: 3, firstName: 'james', lastName: 'bond' }
    ]
    
    46. Định dạng tệp xuất (JSON, CSV hoặc TSV)

Bạn có thể tìm thấy chuỗi kết nối của cụm bằng cách tham khảo chi tiết kết nối của cụm của bạn

Lệnh

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
42 sử dụng cú pháp sau

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

Một lệnh ví dụ trông như thế này

mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
0

Xuất trả hàng thành công

mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
1

Để tải lên đồng thời nhiều cơ sở dữ liệu, hãy xem lệnh

test> db.employees.find()
[
  { _id: 2, firstName: 'bill', lastName: 'gates' },
  { _id: 1, firstName: 'steve', lastName: 'jobs' },
  { _id: 3, firstName: 'james', lastName: 'bond' }
]
42 và
mongoimport --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true"  --collection test  test.json
3. Xem tài liệu của MongoDB để tìm hiểu thêm về cách nhập dữ liệu hoặc truy vấn bộ sưu tập của bạn