MongoDB hoạt động tra cứu như thế nào?
Đây là hướng dẫn về Tra cứu trong MongoDB. Ở đây chúng ta thảo luận về các đặc điểm và ví dụ về Tra cứu trong MongoDB cùng với truy vấn và đầu ra. Bạn cũng có thể xem qua các bài viết được đề xuất khác của chúng tôi để tìm hiểu thêm –
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 Show
Giai đoạn tổng hợp MongoDB $lookupGiai đ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ợpMộ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 EditorTrong 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ớiNhấ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ống4 – Chạy truy vấn tổng hợpNhậ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" }
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 mongoTrì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ỏ mongoTrong 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ợpTrong 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 Khi nào sử dụng tra cứu trong MongoDB?Toán tử $lookup là toán tử tổng hợp hoặc giai đoạn tổng hợp, được sử dụng để nối tài liệu từ một tập hợp này với tài liệu của một tập hợp khác của cùng một cơ sở dữ liệu dựa trên một số . Cả hai bộ sưu tập phải thuộc cùng một cơ sở dữ liệu. . Both the collections should belong to the same databases.
Làm cách nào để lấy dữ liệu từ 2 bộ sưu tập trong MongoDB?Để thực hiện MongoDB Tham gia hai bộ sưu tập, bạn phải sử dụng toán tử tra cứu $ . Nó được định nghĩa là một giai đoạn thực hiện nối ngoài bên trái với một bộ sưu tập khác và hỗ trợ lọc dữ liệu từ các tài liệu đã nối. Ví dụ: nếu người dùng yêu cầu tất cả các điểm từ tất cả học sinh, thì truy vấn bên dưới có thể được viết. Sinh viên.
$root trong MongoDB là gì?$$ROOT. Biến $$ROOT chứa tài liệu nguồn cho nhóm .
Tại sao lại sử dụng thư giãn trong MongoDB?Toán tử giai đoạn $unwind MongoDB được sử dụng để giải cấu trúc trường mảng từ tài liệu đầu vào để xuất tài liệu cho từng phần tử . Mọi tài liệu đầu ra là tài liệu đầu vào với giá trị của trường mảng được thay thế bằng phần tử. Những điểm cần nhớ. Nếu giá trị của một trường không phải là một mảng, db. |