Hướng dẫn tra cứu $ về MongoDB này là phần đầu tiên trong ví dụ về truy vấn tổng hợp gồm ba phần sử dụng các giai đoạn
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];3,
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];4 &
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];5. Tải xuống Studio 3T để bắt đầu
Giai đoạn tổng hợp MongoDB $lookup
Giai đoạn quy trình tổng hợp
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];3 cho phép kết hợp dữ liệu từ tập hợp đầu vào [tập hợp bạn đang chạy truy vấn] và tập hợp tra cứu [tập hợp bạn muốn lấy dữ liệu], miễn là cả hai tập hợp đều nằm trên cùng một cơ sở dữ liệu
Tài liệu đầu ra từ bộ sưu tập tra cứu được thêm dưới dạng tài liệu nhúng trong bộ sưu tập đầu vào
Trong hướng dẫn này, chúng tôi sẽ sử dụng tập dữ liệu
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];7 làm bộ sưu tập đầu vào của mình [tải xuống. json tại đây] và bộ dữ liệu
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];8 giá cả phải chăng ở Chicago có sẵn công khai dưới dạng bộ sưu tập tra cứu của chúng tôi [tải xuống. json ở đây]
Tiếp theo, nhập hai bộ dữ liệu theo các hướng dẫn này và đảm bảo rằng chúng nằm trên cùng một cơ sở dữ liệu
Chúng tôi sẽ sử dụng một ví dụ so khớp bình đẳng để minh họa giai đoạn
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];3. Khớp đẳng thức yêu cầu bộ sưu tập đầu vào và bộ sưu tập tra cứu có một trường để khớp trên
[Trong trường hợp của chúng tôi, đây là trường
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];0 được tìm thấy trong
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];7 và trường
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];2 trong
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];8. ]
Chúng tôi muốn hiển thị các tùy chọn nhà ở giá cả phải chăng trong tập dữ liệu
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];7 dưới dạng trường nhúng mới
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];5, nơi có mã zip khớp
Chức năng tra cứu $ MongoDB trong Trình chỉnh sửa tổng hợp
Một đường ống tổng hợp có nghĩa là có nhiều giai đoạn
Nếu bạn đang xây dựng các truy vấn tổng hợp nhiều giai đoạn, phức tạp hơn, chúng tôi khuyên bạn nên sử dụng Trình chỉnh sửa tổng hợp. Nó cho phép bạn xây dựng các truy vấn theo từng giai đoạn, kiểm tra đầu vào và đầu ra, thêm, di chuyển hoặc xóa các giai đoạn khi bạn thực hiện và xem truy vấn của bạn trong mã shell mongo đầy đủ
Nhận Trình chỉnh sửa tổng hợp bằng cách tải xuống phiên bản Studio 3T mới nhất tại đây
1 – Nhấp chuột phải vào bộ sưu tập đầu vào và chọn Open Aggregation Editor
Trong ví dụ của chúng tôi, bộ sưu tập đầu vào là
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];7
2 – Thêm một giai đoạn mới
Nhấp vào biểu tượng dấu cộng màu xanh lục trên thanh công cụ hoặc thêm liên kết giai đoạn mới trong Quy trình quy trình
Thao tác này sẽ mở một tab có tên Giai đoạn 1
3 – Chọn $lookup từ menu thả xuống
4 – Chạy truy vấn tổng hợp
Nhập lệnh của bạn vào phần Trình chỉnh sửa sân khấu, theo cú pháp được mô tả
Giai đoạn 1 với MongoDB
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];3 sẽ như thế nào
{ "from" : "housing", "localField" : "address.zip_code", "foreignField" : "Zip Code", "as" : "address.zip_code.affordable_housing_options" }
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];
8 là bộ sưu tập tra cứu của chúng tôidb.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];
0 là tên trường trong bộ sưu tập đầu vào của chúng tôidb.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];
2 là tên trường trong bộ sưu tập tra cứu của chúng tôi- Chúng tôi muốn tạo một trường mới
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];
5 trong bộ sưu tập đầu vào của chúng tôi. Đây là nơi chúng tôi sẽ nhúng các tài liệu đầu ra từ bộ sưu tập tra cứu của chúng tôi
Thực hiện truy vấn bằng cách nhấp vào nút phát [Thực hiện toàn bộ đường dẫn] trên thanh công cụ
5 – Kiểm tra kết quả của bạn
Để kiểm tra trực quan nhanh, cách tốt nhất là sử dụng Table View
Nhấp vào bất kỳ đâu trên tab Kết quả và chọn Hiển thị tất cả các trường được nhúng. Điều này mở rộng tất cả các trường được nhúng của
db.getCollection["customers"].aggregate[ [ { "$lookup" : { "from" : "housing", "localField" : "address.zip_code", "foreignField" : "Zip Code", "as" : "address.zip_code.affordable_housing_options" } } ] ];2 trong cùng một chế độ xem
Bạn sẽ thấy lĩnh vực mới của chúng tôi,
db.getCollection["customers"].aggregate[ [ { "$lookup" : { "from" : "housing", "localField" : "address.zip_code", "foreignField" : "Zip Code", "as" : "address.zip_code.affordable_housing_options" } } ] ];3
Nhớ. Chúng tôi chỉ có thông tin nhà ở giá cả phải chăng cho mã vùng Chicago, vì vậy không phải khách hàng nào cũng có giá trị cho trường mới này
Nhấp vào tiêu đề cột để sắp xếp kết quả hiển thị theo thứ tự giảm dần
Cuộn sang bên trái và bạn sẽ thấy Maria Kelly là kết quả đầu tiên, với 2 yếu tố
Nhấp chuột phải vào mục nhập và chọn Tài liệu > Xem tài liệu [JSON]
Bạn sẽ thấy hai lựa chọn nhà ở hợp túi tiền được lồng trong trường
db.getCollection["customers"].aggregate[ [ { "$lookup" : { "from" : "housing", "localField" : "address.zip_code", "foreignField" : "Zip Code", "as" : "address.zip_code.affordable_housing_options" } } ] ];4
6 – Xem toàn bộ mã shell mongo
Trình chỉnh sửa tổng hợp cung cấp giao diện thuận tiện hơn để tạo truy vấn, nhưng bạn luôn có thể xem toàn bộ mã trình bao mongo tại bất kỳ điểm nào trong quy trình
Nhấp vào tab Mã truy vấn và chọn vỏ mongo
Thao tác này sẽ hiển thị cho bạn truy vấn MongoDB đầy đủ mà bạn cũng có thể chạy trong IntelliShell, mongo shell tích hợp sẵn của Studio 3T
Nhấp vào nút Mở trong IntelliShell
Thao tác này trực tiếp mở truy vấn trong tab IntelliShell riêng biệt
Nếu bạn là người chuyên nghiệp trong việc viết các truy vấn MongoDB, thì cũng có một cách khó [hơn] để xây dựng các truy vấn tổng hợp. bằng cách viết thủ công chúng trong trình bao
Chức năng tra cứu $ MongoDB trong vỏ mongo
Trong trường hợp bạn chưa có, hãy tải xuống bộ dữ liệu
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];7 dưới dạng bộ sưu tập đầu vào [tải xuống] và bộ dữ liệu
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];8 giá cả phải chăng có sẵn ở Chicago dưới dạng bộ sưu tập tra cứu [tải xuống]. Nhập hai bộ dữ liệu theo các hướng dẫn này và đảm bảo rằng chúng nằm trên cùng một cơ sở dữ liệu
Nếu bạn đang chạy một truy vấn tổng hợp sử dụng MongoDB
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];3, bạn có thể thực hiện trực tiếp trong IntelliShell bằng cú pháp sau
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];
1- Nhấp chuột phải vào cơ sở dữ liệu của bạn và chọn Open IntelliShell
Đảm bảo rằng đó là cơ sở dữ liệu chứa các bộ sưu tập của bạn [trong ví dụ của chúng tôi là
db.getCollection["customers"].aggregate[ [ { "$lookup" : { "from" : "housing", "localField" : "address.zip_code", "foreignField" : "Zip Code", "as" : "address.zip_code.affordable_housing_options" } } ] ];8]
2 – Chạy truy vấn tổng hợp
Trong phần Trình chỉnh sửa, dán truy vấn tổng hợp
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];3
Truy vấn của chúng tôi trong trường hợp này là
db.getCollection["customers"].aggregate[ [ { "$lookup" : { "from" : "housing", "localField" : "address.zip_code", "foreignField" : "Zip Code", "as" : "address.zip_code.affordable_housing_options" } } ] ];
Sau đó, nhấp vào Thực thi toàn bộ tập lệnh trên thanh công cụ
3 – Kiểm tra kết quả của bạn
Điều này sẽ cung cấp cho bạn kết quả tương tự như Bước 5 từ Trình chỉnh sửa tổng hợp
Như bạn sẽ thấy trong các ví dụ sau, việc viết các truy vấn tổng hợp MongoDB theo cách thủ công có thể khá cồng kềnh. Gỡ lỗi, khi bạn thêm nhiều giai đoạn, sẽ khó khăn hơn nếu không có cách nhanh chóng để kiểm tra đầu vào và đầu ra, đây là một tính năng có sẵn trong Trình chỉnh sửa tổng hợp
Tiếp theo, chúng tôi sẽ thêm giai đoạn thứ hai -
db.getCollection["insert-database-name-here"].aggregate[ [ { "$lookup" : { from: "name of lookup collection", localField: "field in the input collection", foreignField: "field in the lookup collection", as: "field name for the output documents" } } ] ];4 - để lọc tài liệu của chúng tôi để chỉ hiển thị các trường chúng tôi muốn