Tôi đang sử dụng mã JS gần như pure để tham gia ba bộ sưu tập trong MongoDB và tôi tin rằng phải có một cách dễ dàng hơn để làm điều đó chỉ bằng cách sử dụng truy vấn Mongoose.
Đây là trường hợp của tôi; Tôi có một người bán có thể tạo và bán tài liệu. Tôi muốn người bán của tôi có thể xem các tài liệu mà anh ấy đã bán.
Đây là lược đồ của tôi cho bộ sưu tập đơn hàng:
const OrderSchema = new mongoose.Schema[{
//...
user: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true
},
documents: [
{
document: {
type: mongoose.Schema.ObjectId,
ref: 'Document',
required: true
},
price: {
type: Number,
required: [true, 'Price is required']
}
}
],
//...
}];
Và lược đồ của tôi cho tài liệu trông như thế này:
const DocumentSchema = new mongoose.Schema[{
//...
title: {
type: String,
required: [true, 'Please provide a title'],
},
author: {
type: mongoose.Schema.ObjectId,
ref: 'Seller',
required: true
},
//...
}];
Như tôi đã nói trước đây, tôi muốn có được một danh sách các tài liệu thuộc về người bán đã đăng nhập được bán. [Một cái gì đó như thế này].
"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
Mã tôi có được cho đến nay là cái dưới đây. Nó trông hơi xấu và nó chứa ba fors.
const documents = await Document.find[].where['author', req.uid].select['orders'].populate[{ path: 'orders' }]
let filteredOrders = [];
for [const document of documents] {
//GET ALL ORDERS THAT CONTAINS THIS DOCUMENTS
const orders = await Order.find[].where['documents.document', document._id].populate[{ path: 'documents.document', select: 'title' }].sort['-createdAt'];
for [const order of orders] {
for [const doc of order.documents] {
if [doc.document._id.toString[] == document._id.toString[]] {
filteredOrders.push[{
orderId: order._id,
documentId: doc._id,
title: doc.document.title,
price: doc.price,
createdAt: order.createdAt
}];
}
}
}
}
filteredOrders.sort[[a, b] => new Date[b.createdAt] - new Date[a.createdAt]];
res.status[200].json[{ success: true, data: filteredOrders }];
Có cách nào chỉ với Mongoose mà tôi có thể nhận được kết quả tương tự không?
{ $lookup: { from: , localField: , foreignField: , as: } }
5 không thể bao gồm giai đoạn { $lookup: { from: , let: { : , …, : }, pipeline: [ ], as: } }
8 hoặc giai đoạn { $lookup: { from: , let: { : , …, : }, pipeline: [ ], as: } }
9. Bắt đầu từ V6.0, { $lookup: { from: , localField: , foreignField: , as: } }
5 có thể chứa giai đoạn tìm kiếm Atlas ____101 làm giai đoạn đầu tiên bên trong đường ống. Để tìm hiểu thêm, xem hỗ trợ tìm kiếm Atlas.{ $lookup: { from: , localField: , foreignField: , as: } }
5 không thể truy cập trực tiếp vào các trường tài liệu đã tham gia. Thay vào đó, hãy xác định các biến cho các trường tài liệu đã tham gia bằng cách sử dụng tùy chọn LET và sau đó tham chiếu các biến trong các giai đoạn { $lookup: { from: , localField: , foreignField: , as: } }
5.như
Chỉ định tên của trường mảng mới để thêm vào các tài liệu đã tham gia. Trường mảng mới chứa các tài liệu phù hợp từ bộ sưu tập được tham gia. Nếu tên được chỉ định đã tồn tại trong tài liệu đã tham gia, trường hiện tại sẽ bị ghi đè.
db.sivaUser.insert[
{
"_id" : ObjectId["5684f3c454b1fd6926c324fd"],
"email" : "",
"userId" : "AD",
"userName" : "admin"
}]
//"userinfo"
db.sivaUserInfo.insert[
{
"_id" : ObjectId["56d82612b63f1c31cf906003"],
"userId" : "AD",
"phone" : "0000000000"
}]
//"userrole"
db.sivaUserRole.insert[
{
"_id" : ObjectId["56d82612b63f1c31cf906003"],
"userId" : "AD",
"role" : "admin"
}]
Theo tài liệu, $ Tra cứu chỉ có thể tham gia một bộ sưu tập bên ngoài.
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"
}
}
]]
Những gì bạn có thể làm là kết hợp
const DocumentSchema = new mongoose.Schema[{
//...
title: {
type: String,
required: [true, 'Please provide a title'],
},
author: {
type: mongoose.Schema.ObjectId,
ref: 'Seller',
required: true
},
//...
}];
0 và const DocumentSchema = new mongoose.Schema[{
//...
title: {
type: String,
required: [true, 'Please provide a title'],
},
author: {
type: mongoose.Schema.ObjectId,
ref: 'Seller',
required: true
},
//...
}];
1 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.
[{
$match: {
category_id: mongoose.Types.ObjectId[category_id]
}
},
{
$lookup: {
from: "properties",
localField: "category_id",
foreignField: "category_id",
as: "properties"
}
}
]
Tạo bộ sưu tập
"Tham gia" tất cả họ :-]
Tôi có hai bộ sưu tập,
const DocumentSchema = new mongoose.Schema[{
//...
title: {
type: String,
required: [true, 'Please provide a title'],
},
author: {
type: mongoose.Schema.ObjectId,
ref: 'Seller',
required: true
},
//...
}];
2 và const DocumentSchema = new mongoose.Schema[{
//...
title: {
type: String,
required: [true, 'Please provide a title'],
},
author: {
type: mongoose.Schema.ObjectId,
ref: 'Seller',
required: true
},
//...
}];
3.Tôi đang tìm kiếm một cái nhìn như: → MongoDB Manual
Điều này về cơ bản giúp tôi có được tất cả các sản phẩm phù hợp với Category_id và bao gồm các thuộc tính phù hợp với cùng một danh mục_id.
- Tôi cần thêm một kiểm tra bổ sung, cho
4 trên kết quảconst DocumentSchema = new mongoose.Schema[{ //... title: { type: String, required: [true, 'Please provide a title'], }, author: { type: mongoose.Schema.ObjectId, ref: 'Seller', required: true }, //... }];
3. Trong các từ khác,const DocumentSchema = new mongoose.Schema[{ //... title: { type: String, required: [true, 'Please provide a title'], }, author: { type: mongoose.Schema.ObjectId, ref: 'Seller', required: true }, //... }];
3 nên được nhóm lại bởiconst DocumentSchema = new mongoose.Schema[{ //... title: { type: String, required: [true, 'Please provide a title'], }, author: { type: mongoose.Schema.ObjectId, ref: 'Seller', required: true }, //... }];
4 được trả về từ bộ sưu tậpconst DocumentSchema = new mongoose.Schema[{ //... title: { type: String, required: [true, 'Please provide a title'], }, author: { type: mongoose.Schema.ObjectId, ref: 'Seller', required: true }, //... }];
2 và phù hợp với cùng một khóa trongconst DocumentSchema = new mongoose.Schema[{ //... title: { type: String, required: [true, 'Please provide a title'], }, author: { type: mongoose.Schema.ObjectId, ref: 'Seller', required: true }, //... }];
3. Điều đó có ý nghĩa? Về cơ bản có khả năng có nhiều định nghĩa địa phương/nước ngoài.const DocumentSchema = new mongoose.Schema[{ //... title: { type: String, required: [true, 'Please provide a title'], }, author: { type: mongoose.Schema.ObjectId, ref: 'Seller', required: true }, //... }];
- Bất kỳ ý tưởng làm thế nào tôi có thể điều này?
- Tài liệu về nhà → Hướng dẫn sử dụng MongoDB
- Trên trang này
- Sự định nghĩa
- Cú pháp
- Phù hợp với bình đẳng với một điều kiện tham gia duy nhất
- Tham gia các điều kiện và các nhóm phụ trên một bộ sưu tập được tham gia
- Các nhóm phụ tương quan bằng cách sử dụng cú pháp ngắn gọn
- Hành vi
- Quan điểm và đối chiếu
- Ví dụ
- Thực hiện một sự bình đẳng duy nhất với
0"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
- Sử dụng
0 với một mảng"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
- Sử dụng
0 với"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
3"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
- Thực hiện nhiều lần tham gia và một trình điều khiển tương quan với
0"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
- Thực hiện một trình điều khiển không tương thích với
0"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
- Thực hiện một trình độ con tương quan ngắn gọn với
0"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0Đã thay đổi trong phiên bản 5.1.
Thực hiện tham gia bên ngoài bên trái vào một bộ sưu tập trong cùng một cơ sở dữ liệu để lọc trong các tài liệu từ bộ sưu tập "được nối" để xử lý. Giai đoạn
"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 thêm một trường mảng mới vào mỗi tài liệu đầu vào. Trường mảng mới chứa các tài liệu phù hợp từ bộ sưu tập "được tham gia". Giai đoạn "data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 vượt qua các tài liệu được định hình lại này cho giai đoạn tiếp theo."data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 stage adds a new array field to each input document. The new array field contains the matching documents from the "joined" collection. The "data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 stage passes these reshaped documents to the next stage.Bắt đầu từ MongoDB 5.1,
"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 hoạt động trên các bộ sưu tập Sharded."data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 works across sharded collections.Để kết hợp các yếu tố từ hai bộ sưu tập khác nhau, hãy sử dụng giai đoạn đường ống
const documents = await Document.find[].where['author', req.uid].select['orders'].populate[{ path: 'orders' }]
let filteredOrders = [];
for [const document of documents] {
//GET ALL ORDERS THAT CONTAINS THIS DOCUMENTS
const orders = await Order.find[].where['documents.document', document._id].populate[{ path: 'documents.document', select: 'title' }].sort['-createdAt'];
for [const order of orders] {
for [const doc of order.documents] {
if [doc.document._id.toString[] == document._id.toString[]] {
filteredOrders.push[{
orderId: order._id,
documentId: doc._id,
title: doc.document.title,
price: doc.price,
createdAt: order.createdAt
}];
}
}
}
}
filteredOrders.sort[[a, b] => new Date[b.createdAt] - new Date[a.createdAt]];
res.status[200].json[{ success: true, data: filteredOrders }];
1.Giai đoạn
"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 có các cú pháp sau:"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 stage has the
following syntaxes:Để thực hiện một sự phù hợp bình đẳng giữa một trường từ các tài liệu đầu vào với một trường từ các tài liệu của bộ sưu tập "đã tham gia", giai đoạn
"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 có cú pháp này:"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 stage has this syntax:{ $lookup: { from: , localField: , foreignField: , as: } }
"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 lấy một tài liệu với các trường này:"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 takes a document with these fields:
từ | Chỉ định bộ sưu tập trong cùng một cơ sở dữ liệu để thực hiện tham gia với. 5 là tùy chọn, bạn có thể sử dụng giai đoạn 6 trong giai đoạn 0 thay thế. Ví dụ, xem Sử dụng giai đoạn 6 trong giai đoạn 0. 0 stage instead. For an example, see Use a 6 Stage in a 0 Stage.Bắt đầu từ MongoDB 5.1, bộ sưu tập được chỉ định trong tham số 5 có thể được chia cắt.
|
địa phương | Chỉ định trường từ đầu vào tài liệu đến giai đoạn 0. 0 thực hiện một trận đấu bình đẳng trên 3 với 4 từ các tài liệu của bộ sưu tập 5. Nếu một tài liệu đầu vào không chứa 3, 0 coi trường có giá trị 8 cho mục đích phù hợp. 0 stage. 0 performs an equality match on the 3 to the 4 from the documents of the 5 collection. If an input document does not contain the 3, the 0 treats the field as having a value of 8 for matching purposes.
|
vùng ngoại quốc | Chỉ định trường từ các tài liệu trong bộ sưu tập 5. 0 thực hiện phù hợp với sự bình đẳng trên 4 với 3 từ các tài liệu đầu vào. Nếu một tài liệu trong bộ sưu tập 5 không chứa 4, 0 coi giá trị là 8 cho các mục đích phù hợp. 0 performs an equality match on the 4 to the 3 from the input documents. If a document in the 5 collection does not contain the 4, the 0 treats the value as 8 for matching purposes.
|
như | Chỉ định tên của trường mảng mới để thêm vào các tài liệu đầu vào. Trường mảng mới chứa các tài liệu phù hợp từ bộ sưu tập 5. Nếu tên được chỉ định đã tồn tại trong tài liệu đầu vào, trường hiện tại sẽ được ghi đè.
|
Hoạt động sẽ tương ứng với câu lệnh Pseudo-SQL sau:
SELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
Xem các ví dụ sau:
Thực hiện một sự bình đẳng duy nhất với
0"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
Sử dụng
0 với một mảng"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
Sử dụng
0 với"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
3"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
Thực hiện nhiều lần tham gia và một trình điều khiển tương quan với
"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0Thực hiện một trình điều khiển không tương thích với
0"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
Thực hiện một trình độ con tương quan ngắn gọn với
0"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
Đã thay đổi trong phiên bản 5.1.
Thực hiện tham gia bên ngoài bên trái vào một bộ sưu tập trong cùng một cơ sở dữ liệu để lọc trong các tài liệu từ bộ sưu tập "được nối" để xử lý. Giai đoạn
"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 thêm một trường mảng mới vào mỗi tài liệu đầu vào. Trường mảng mới chứa các tài liệu phù hợp từ bộ sưu tập "được tham gia". Giai đoạn "data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 vượt qua các tài liệu được định hình lại này cho giai đoạn tiếp theo.pipeline in a "data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 stage that references document fields from a joined collection. An uncorrelated subquery does not reference joined fields.Bắt đầu từ MongoDB 5.1, "data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 hoạt động trên các bộ sưu tập Sharded.
"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
Để kết hợp các yếu tố từ hai bộ sưu tập khác nhau, hãy sử dụng giai đoạn đường ống
const documents = await Document.find[].where['author', req.uid].select['orders'].populate[{ path: 'orders' }]
let filteredOrders = [];
for [const document of documents] {
//GET ALL ORDERS THAT CONTAINS THIS DOCUMENTS
const orders = await Order.find[].where['documents.document', document._id].populate[{ path: 'documents.document', select: 'title' }].sort['-createdAt'];
for [const order of orders] {
for [const doc of order.documents] {
if [doc.document._id.toString[] == document._id.toString[]] {
filteredOrders.push[{
orderId: order._id,
documentId: doc._id,
title: doc.document.title,
price: doc.price,
createdAt: order.createdAt
}];
}
}
}
}
filteredOrders.sort[[a, b] => new Date[b.createdAt] - new Date[a.createdAt]];
res.status[200].json[{ success: true, data: filteredOrders }];
1."data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 pipeline stage containing a [{
$match: {
category_id: mongoose.Types.ObjectId[category_id]
}
},
{
$lookup: {
from: "properties",
localField: "category_id",
foreignField: "category_id",
as: "properties"
}
}
]
4 stage, the [{
$match: {
category_id: mongoose.Types.ObjectId[category_id]
}
},
{
$lookup: {
from: "properties",
localField: "category_id",
foreignField: "category_id",
as: "properties"
}
}
]
5 operator, or the [{
$match: {
category_id: mongoose.Types.ObjectId[category_id]
}
},
{
$lookup: {
from: "properties",
localField: "category_id",
foreignField: "category_id",
as: "properties"
}
}
]
6 operator, the subquery is always run again if repeated.
Previously, depending on the subquery output size, either the subquery output was cached or the subquery was run again.Giai đoạn
"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 có các cú pháp sau:Để thực hiện một sự phù hợp bình đẳng giữa một trường từ các tài liệu đầu vào với một trường từ các tài liệu của bộ sưu tập "đã tham gia", giai đoạn
"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 có cú pháp này:concise correlated subqueries."data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 lấy một tài liệu với các trường này:"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 syntax:{ $lookup: { from: , let: { : , …, : }, pipeline: [ ], as: } }
Đồng ruộng
0 stage accepts a document with these fields:"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
từ | Chỉ định bộ sưu tập trong cùng một cơ sở dữ liệu để thực hiện tham gia với. 5 là tùy chọn, bạn có thể sử dụng giai đoạn 6 trong giai đoạn 0 thay thế. Ví dụ, xem Sử dụng giai đoạn 6 trong giai đoạn 0. 0 stage instead. For an example, see Use a 6 Stage in a 0 Stage.Bắt đầu từ MongoDB 5.1, bộ sưu tập được chỉ định trong tham số 5 có thể được chia cắt.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let | địa phươngpipeline stages. Use the variable expressions to access the fields from the joined collection's documents that are input to the 5.Bắt đầu từ MongoDB 5.1, "data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
] |
{ |
$lookup: |
{ |
from: , |
localField: , |
foreignField: , |
as: |
} |
} |
Giai đoạn
"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 có các cú pháp sau:let variables can be accessed by the stages in the pipeline, including additional "data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 stages nested in the { $lookup: { from: , localField: , foreignField: , as: } }
5.Để thực hiện một sự phù hợp bình đẳng giữa một trường từ các tài liệu đầu vào với một trường từ các tài liệu của bộ sưu tập "đã tham gia", giai đoạn
0 có cú pháp này:"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
Bắt đầu từ MongoDB 5.0,
3,SELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
4,SELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
5,SELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
6 vàSELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
7 Các toán tử so sánh được đặt trong toán tửSELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
0 có thể sử dụng chỉ số trên bộ sưu tậpSELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
5 được tham chiếu trong giai đoạn ____. Hạn chế:const documents = await Document.find[].where['author', req.uid].select['orders'].populate[{ path: 'orders' }] let filteredOrders = []; for [const document of documents] { //GET ALL ORDERS THAT CONTAINS THIS DOCUMENTS const orders = await Order.find[].where['documents.document', document._id].populate[{ path: 'documents.document', select: 'title' }].sort['-createdAt']; for [const order of orders] { for [const doc of order.documents] { if [doc.document._id.toString[] == document._id.toString[]] { filteredOrders.push[{ orderId: order._id, documentId: doc._id, title: doc.document.title, price: doc.price, createdAt: order.createdAt }]; } } } } filteredOrders.sort[[a, b] => new Date[b.createdAt] - new Date[a.createdAt]]; res.status[200].json[{ success: true, data: filteredOrders }];
0 stage. Limitations:"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
Các chỉ mục đa năng không được sử dụng.
Các chỉ mục không được sử dụng để so sánh trong đó toán hạng là một mảng hoặc loại toán hạng không được xác định.
Các chỉ mục không được sử dụng để so sánh với nhiều hơn một toán hạng đường dẫn trường.
Các giai đoạn khác [không phải là 79] trong đường ống không yêu cầu toán tử
0 để truy cập các biến.pipeline do not require anSELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
0 operator to access the variables.SELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
đường ống
Chỉ định
{ $lookup: { from: , localField: , foreignField: , as: } }
5 để chạy trên bộ sưu tập đã tham gia. { $lookup: { from: , localField: , foreignField: , as: } }
5 xác định các tài liệu kết quả từ bộ sưu tập được tham gia. Để trả về tất cả các tài liệu, chỉ định một { $lookup: { from: , localField: , foreignField: , as: } }
5 { $lookup: { from: , let: { : , …, : }, pipeline: [ ], as: } }
6 trống.{ $lookup: { from: , localField: , foreignField: , as: } }
5 không thể bao gồm giai đoạn { $lookup: { from: , let: { : , …, : }, pipeline: [ ], as: } }
8 hoặc giai đoạn { $lookup: { from: , let: { : , …, : }, pipeline: [ ], as: } }
9. Bắt đầu từ V6.0, { $lookup: { from: , localField: , foreignField: , as: } }
5 có thể chứa giai đoạn tìm kiếm Atlas ____101 làm giai đoạn đầu tiên bên trong đường ống. Để tìm hiểu thêm, xem hỗ trợ tìm kiếm Atlas.Atlas Search const DocumentSchema = new mongoose.Schema[{
//...
title: {
type: String,
required: [true, 'Please provide a title'],
},
author: {
type: mongoose.Schema.ObjectId,
ref: 'Seller',
required: true
},
//...
}];
01 stage as the first stage inside the pipeline. To learn more, see
Atlas Search Support.{ $lookup: { from: , localField: , foreignField: , as: } }
5 không thể truy cập trực tiếp vào các trường tài liệu đã tham gia. Thay vào đó, hãy xác định các biến cho các trường tài liệu đã tham gia bằng cách sử dụng tùy chọn LET và sau đó tham chiếu các biến trong các giai đoạn { $lookup: { from: , localField: , foreignField: , as: } }
5.let option and then reference the variables in the { $lookup: { from: , localField: , foreignField: , as: } }
5 stages.Ghi chú
Để tham chiếu các biến trong các giai đoạn đường ống, hãy sử dụng cú pháp
{ $lookup: { from: , localField: , foreignField: , as: } }
6.pipeline stages, use the { $lookup: { from: , localField: , foreignField: , as: } }
6 syntax.Các biến LET có thể được truy cập bởi các giai đoạn trong đường ống, bao gồm các giai đoạn
"data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 bổ sung được lồng trong { $lookup: { from: , localField: , foreignField: , as: } }
5.let variables can be accessed by the stages in the pipeline, including additional "data": [
{
"orderId": "606b448dd2d9d643a811bc33",
"documentId": "606b448dd2d9d643a811bc34",
"title": "Document 1 u1",
"price": 90,
"createdAt": "2021-04-05T17:10:37.469Z"
},
{
"orderId": "606b43d2cd9b2740b8b8974b",
"documentId": "606b43d2cd9b2740b8b8974c",
"title": "Business Affiliate u7",
"price": 222,
"createdAt": "2021-04-05T17:07:30.859Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bef",
"title": "Business Affiliate u7",
"price": 777,
"createdAt": "2021-04-05T14:09:07.539Z"
},
{
"orderId": "606b1a03ec048e0d44cb3bee",
"documentId": "606b1a03ec048e0d44cb3bf0",
"title": "Doc 1 u1",
"price": 1,
"createdAt": "2021-04-05T14:09:07.539Z"
},
]
0 stages
nested in the { $lookup: { from: , localField: , foreignField: , as: } }
5.Giai đoạn
9 yêu cầu sử dụng toán tử{ $lookup: { from: , localField: , foreignField: , as: } }
0 để truy cập các biến. Toán tửSELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
0 cho phép sử dụng các biểu thức tổng hợp bên trong cú phápSELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
9.{ $lookup: { from: , localField: , foreignField: , as: } } Bắt đầu từ MongoDB 5.0,
3,SELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
4,SELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
5,SELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
6 vàSELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
7 Các toán tử so sánh được đặt trong toán tửSELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
0 có thể sử dụng chỉ số trên bộ sưu tậpSELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
5 được tham chiếu trong giai đoạn ____. Hạn chế:const documents = await Document.find[].where['author', req.uid].select['orders'].populate[{ path: 'orders' }] let filteredOrders = []; for [const document of documents] { //GET ALL ORDERS THAT CONTAINS THIS DOCUMENTS const orders = await Order.find[].where['documents.document', document._id].populate[{ path: 'documents.document', select: 'title' }].sort['-createdAt']; for [const order of orders] { for [const doc of order.documents] { if [doc.document._id.toString[] == document._id.toString[]] { filteredOrders.push[{ orderId: order._id, documentId: doc._id, title: doc.document.title, price: doc.price, createdAt: order.createdAt }]; } } } } filteredOrders.sort[[a, b] => new Date[b.createdAt] - new Date[a.createdAt]]; res.status[200].json[{ success: true, data: filteredOrders }];
0 stage. Limitations:"data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]
Các chỉ mục đa năng không được sử dụng.
Các chỉ mục không được sử dụng để so sánh trong đó toán hạng là một mảng hoặc loại toán hạng không được xác định.
Các chỉ mục không được sử dụng để so sánh với nhiều hơn một toán hạng đường dẫn trường.
Các giai đoạn khác [không phải là 79] trong đường ống không yêu cầu toán tử
0 để truy cập các biến.pipeline do not require anSELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
0 operator to access the variables.SELECT *, FROM collection WHERE IN [ SELECT * FROM WHERE = ];
đường ống
Chỉ định
{ $lookup: { from: , localField: , foreignField: , as: } }
5 để chạy trên bộ sưu tập đã tham gia. { $lookup: { from: , localField: , foreignField: , as: } }
5 xác định các tài liệu kết quả từ bộ sưu tập được tham gia. Để trả về tất cả các tài liệu, chỉ định một { $lookup: { from: , localField: , foreignField: , as: } }
5 { $lookup: { from: , let: { : , …, : }, pipeline: [ ], as: } }
6 trống.