完全披露:我是 MongoDB 菜鳥
我正在處理一個遺留的資料庫結構。我的 MongoDB 的一部分目前看起來像這樣:
- 事件(_id,名稱(字串),...)
- Orders (_id, eventId (as string), products (array of {prodIdentifier (string), quantity (number)}), customer_ID (string), signee (string), sign_time (date), ...)
- Products (_id, prodIdentifier (string), price (number), sku (string), ...)
關系如下:
- 事件 1..N 訂單(通過 eventId)
- 訂單 1..N 個產品(通過產品陣列)
我需要以給定 eventId 的方式進行查詢,然后回傳
訂單ID
客戶名稱(可以是級聯請求/前端預謀)、
產品SKU、
產品名稱、
數量、
價值(數量*價格)、
簽字人姓名、
簽字時間
請注意,我的界面需要對上述所有欄位進行過濾和排序,以及分頁的限制和偏移,以減少查詢時間、快速 UI 等。
我可以populate在訂單上使用,但是我應該如何通過貓鼬來兌現限制和抵消。我想知道是否應該制作一個視圖,在這種情況下,我應該如何將其展平以發送/接收一個尊重限制和偏移的串列。
或者它必須是一個非常手動的、逐步構建的結果串列?
更新:
資料庫中的樣本資料:
事件物件:
{
"_id" : ObjectId("6218b9266487367ba1c20258"),
"name" : "XYZ",
"createdAt" : ISODate("2022-02-03T13:25:43.814 0000"),
"updatedAt" : ISODate("2022-02-14T09:34:47.819 0000"),
...
}
訂單):
[
{
"_id" : ObjectId("613ae653d0112f6b49fdd437"),
"orderItems" : [
{
"quantity" : NumberInt(2),
"productCode" : "VEO001",
},
{
"quantity" : NumberInt(2),
"productCode" : "VEO002",
},
{
"quantity" : NumberInt(1),
"productCode" : "VEO003",
}
],
"orderCode" : "1000",
"customerCode" : "Customer 1",
"createdAt" : ISODate("2021-09-10T05:00:03.496 0000"),
"updatedAt" : ISODate("2022-02-08T10:06:42.255 0000"),
"eventId" : "6218b9266487367ba1c20258"
}
]
產品:
[
{
"_id" : ObjectId("604206685f25b8560a1cd48d"),
"Product name" : "ABC",
"createdAt" : ISODate("2021-03-05T10:22:32.085 0000"),
"tag" : "VEO001",
"updatedAt" : ISODate("2022-03-28T07:29:21.939 0000"),
"Product Price" : NumberInt(0),
"photo" : {
"_id" : ObjectId("6042071a5f25b8560a1cd4a9"),
"key" : "e8c9a085-4e8d-4ac4-84e9-bb0a83a59145",
"name" : "Screenshot 2021-03-05 at 11.24.50.png"
},
"name" : "ABC",
"_costprice" : NumberInt(12),
"_sku" : "SKUVEO001",
},
{
"_id" : ObjectId("604206685f25b8560a1cd48a"),
"Product name" : "DEF",
"createdAt" : ISODate("2021-03-05T10:22:32.085 0000"),
"tag" : "VEO002",
"updatedAt" : ISODate("2022-03-28T07:29:21.939 0000"),
"Product Price" : NumberInt(0),
"photo" : {
"_id" : ObjectId("6042071a5f25b8560a1cd4a9"),
"key" : "e8c9a085-4e8d-4ac4-84e9-bb0a83a59145",
"name" : "Screenshot 2021-03-05 at 11.24.50.png"
},
"name" : "DEF",
"_costprice" : NumberInt(13),
"_sku" : "SKUVEO002",
},
{
"_id" : ObjectId("604206685f25b8560a1cd48a"),
"Product name" : "GHI",
"createdAt" : ISODate("2021-03-05T10:22:32.085 0000"),
"tag" : "VEO003",
"updatedAt" : ISODate("2022-03-28T07:29:21.939 0000"),
"Product Price" : NumberInt(0),
"photo" : {
"_id" : ObjectId("6042071a5f25b8560a1cd4a9"),
"key" : "e8c9a085-4e8d-4ac4-84e9-bb0a83a59145",
"name" : "Screenshot 2021-03-05 at 11.24.50.png"
},
"name" : "GHI",
"_costprice" : NumberInt(13),
"_sku" : "SKUVEO003",
},
]
預期輸出:

uj5u.com熱心網友回復:
您可以執行以下操作:
db.orders.aggregate([
{$match: {eventId: "6218b9266487367ba1c20258"}},
{
$lookup: {
from: "products",
localField: "orderItems.productCode",
foreignField: "tag",
as: "orderItemsB"
}
},
{
"$addFields": {
"orderItems": {
"$map": {
"input": "$orderItemsB",
"in": {
"$mergeObjects": [
"$$this",
{
"$arrayElemAt": [
"$orderItems",
{"$indexOfArray": ["$orderItems.productCode", "$$this.tag"]}
]
}
]
}
}
},
orderItemsB: 0
}
},
{
$unset: "orderItemsB"
},
{
$lookup: {
from: "events",
let: {eventId: "$eventId"},
pipeline: [
{
$match: {$expr: {$eq: [{$toString: "$_id"}, "$$eventId"]}}
}
],
as: "event"
}
},
{
$set: {event: {"$arrayElemAt": ["$event", 0]}}
},
{$unwind: "$orderItems"}
])
正如您在這個游樂場示例中看到的那樣。這將為您提供包含所有資料的訂單每個產品的檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/471284.html
上一篇:僅當使用貓鼬的mongodb中的陣列中不存在ObjectID時,如何添加ObjectId?
下一篇:計算正確的音高
