Hướng dẫn mongodb join query - truy vấn tham gia mongodb
Theo tài liệu, $ Tra cứu chỉ có thể tham gia một bộ sưu tập bên ngoài. Show
Nội phân chính
Nội phân chính
Những gì bạn có thể làm là kết hợp 0 trong một bộ sưu tập, như ví dụ được cung cấp dựa trên lược đồ DB quan hệ. Mongo là cơ sở dữ liệu NoQuery - và điều này đòi hỏi cách tiếp cận khác nhau để quản lý tài liệu.
Vui lòng tìm truy vấn dưới 2 bước, kết hợp UserInfo với Userrole - tạo bộ sưu tập tạm thời mới được sử dụng trong truy vấn cuối cùng để hiển thị dữ liệu kết hợp. Trong truy vấn cuối cùng có một tùy chọn để sử dụng $ out và tạo bộ sưu tập mới với dữ liệu được hợp nhất để sử dụng sau.
Có một số cơ sở dữ liệu NoQuery ngoài kia, nhưng MongoDB được sử dụng phổ biến nhất và nó có sẵn cả dưới dạng dịch vụ đám mây và để triển khai trên các hệ thống tự quản lý. Mặc dù MongoDB không phải là RDBMS (Hệ thống quản lý cơ sở dữ liệu quan hệ) và không cung cấp các phương thức tham gia rộng rãi, bạn có thể sử dụng một số nhà khai thác khác để tham gia hai bộ sưu tập trong MongoDB. & NBSP;Cloud Service and for Deployment on Self-Managed Systems. Even though MongoDB isn’t an RDBMS (Relational Database Management System) and doesn’t offer extensive join methods, you can use several other operators to join two collections in MongoDB. Khi hoàn chỉnh hướng dẫn của bài viết này, bạn sẽ tìm hiểu chi tiết cách MongoDB tham gia hai bộ sưu tập. Mục lục
MongoDB là gì?Các bước để tham gia hai bộ sưu tập trong MongoDBLàm thế nào MongoDB tham gia hai bộ sưu tập với các điều kiện cụ thể bằng Pipeline & $ Tra cứu?non-relational Database Management System that is Open Source and Cross-Platform. MongoDB, which was founded in 2009, employs the Document-Oriented Database Model to organize data into documents and collections rather than tables. This enables it to store various types of data. Furthermore, it is distributed under the Server Side Public License (SSPL), which supports a novel mechanism for storing and retrieving large amounts of data. MongoDB is a straightforward and simple-to-configure database that provides high performance, automatic scalability, and high availability. Làm thế nào để MongoDB tham gia hai bộ sưu tập với mệnh đề WHERE? Sử dụng la bàn để tham gia hai bộ sưu tập trong MongoDB $lookup (Aggregation) function. $lookup(Aggregation) creates an outer left join with another collection and helps to filter data from merged data. If documents are part of a “joined” collection, the $lookup (Aggregation) function will return documents in the form of a subarray of the original collection. Những gì bạn có thể làm là kết hợp
db.sivaUserInfo.aggregate([
{$lookup:
{
from: "sivaUserRole",
localField: "userId",
foreignField: "userId",
as: "userRole"
}
},
{
$unwind:"$userRole"
},
{
$project:{
"_id":1,
"userId" : 1,
"phone" : 1,
"role" :"$userRole.role"
}
},
{
$out:"sivaUserTmp"
}
])
db.sivaUserTmp.aggregate([
{$lookup:
{
from: "sivaUser",
localField: "userId",
foreignField: "userId",
as: "user"
}
},
{
$unwind:"$user"
},
{
$project:{
"_id":1,
"userId" : 1,
"phone" : 1,
"role" :1,
"email" : "$user.email",
"userName" : "$user.userName"
}
}
])
|