我正在尋找只有一個價格的所有選項 我有兩個系列,系列選項和系列價格
集合選項的結構如下所示:
{
"_id": "option_0",
"id": 0,
"type": "option",
"name": "Stewart",
"price_per_day": true,
"sell_per": "person",
"sell_per_unit_min_capacity": null,
"sell_per_unit_max_capacity": null,
"unit_type": "boardType",
"age_ranges": [
{
"id": 0,
"type": "age_range",
"age_min": 88,
"age_max": 33
},
{
"id": 1,
"type": "age_range",
"age_min": 47,
"age_max": 53
},
{
"id": 2,
"type": "age_range",
"age_min": 78,
"age_max": 54
}
],
"prices": [
"price_54",
"price_824",
"price_811",
"price_19",
"price_130",
"price_855",
"price_437",
"price_131"
],
"stocks": [
"stock_361",
"stock_111",
"stock_267",
"stock_382",
"stock_345",
"stock_262",
"stock_54",
"stock_718"
]
}
收藏價格的結構如下:
{
"_id": "price_0",
"id": 0,
"type": "price",
"is_archived": true,
"name": "Wendi",
"bu": "fr",
"currency": "EUR",
"price_type": "duration",
"commission": "$2,426.70",
"is_commissionable": true,
"data": [
{
"date": "2022-02-13T01:29:29",
"durations": "Glenna Merritt",
"prices": [
{
"age_range_id": "age_range_0",
"value": 22,
"price_tier": [
{
"quantity": 7,
"value": 11
}
]
},
{
"age_range_id": "age_range_2",
"value": 28
}
]
},
{
"date": "2022-07-26T01:14:43",
"durations": "Deanna Blair",
"prices": [
{
"age_range_id": "age_range_0",
"value": 5,
"price_tier": [
{
"quantity": 10,
"value": 6
}
]
},
{
"age_range_id": "age_range_1",
"value": 9
}
]
}
]
}
在收藏價格上,您可以在資料中擁有多個物件或僅一個物件。
我想找到所有 option.prices 包含一個價格的價格。
我怎樣才能在 MongoDB 中做到這一點?
感謝您的幫助
我試圖在這樣的 var 中僅使用一個價格來存盤所有價格:
var pricesWithOnePrice = db.prices.find( {"data": { $size: 1 }} )
我試圖找到所有選項都包含這樣的價格:
db.options.find({"prices": pricesWithOnePrice})
我嘗試了另一個請求,但不起作用:
db.option.aggregate([{$lookup: {from: "prices", localField: "prices", foreignField: "_id", pipeline: [{$match: {"data": {$size:1}}, as: "jointure"}}])
但是我查詢運行但從來沒有給我一個結果。
uj5u.com熱心網友回復:
一種選擇是使用$lookup
管道僅將_id
item 的價格帶入 under data
,以及$match
至少具有這樣一個價格的選項
db.options.aggregate([
{$lookup: {
from: "price",
let: {prices: "$prices"},
pipeline: [
{$match: {
$expr: {$and: [
{$eq: [{$size: "$data"}, 1]},
{$in: ["$_id", "$$prices"]}
]
}
}},
{$project: {_id: 1}}
],
as: "priceObj"
}
},
{$match: {"priceObj.0": {$exists: true}}}
])
看看它在操場上的例子是如何作業的
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/526568.html
標籤:mongodb