在MongoDB中查找只有一个价格的所有选项



我正在寻找只有一个价格的find-all选项我有两个收藏,收藏选项和收藏价格

集合选项的结构如下,例如:

{
"_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
}
]
}
]
}

在收集价格上,您可以在数据中有多个Object或只有一个Object。

我想找到所有的选项。价格中包含一个价格。

如何在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"}}])

但我运行了查询,但从未得到结果。

一个选项是使用$lookup管道只将商品的价格中的_id带到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}}}
])

看看它是如何在操场上工作的例子

相关内容

最新更新