【发布时间】:2020-06-16 00:00:31
【问题描述】:
我有三份文件。第一个是 percentages,其他是 discardeditems 和 filtereditems强>。这些文件的样本数据如下。
db = {
"percentages": [
{
"_id": 1,
"base": "A",
"buy": "BUY_1",
"sell": "SELL_1",
"item": "ITEM_B",
"ask": 100,
"bid": 114,
"percentage": 14
},
{
"_id": 2,
"base": "B",
"buy": "BUY_2",
"sell": "SELL_2",
"item": "ITEM_G",
"ask": 50,
"bid": 90,
"percentage": 80
},
{
"_id": 3,
"base": "A",
"buy": "BUY_2",
"sell": "SELL_2",
"item": "ITEM_G",
"ask": 10,
"bid": 15,
"percentage": 50
}
],
"discardeditems": [
{
"_id": 1,
"buy": "BUY_1",
"sell": "SELL_1",
"item": "ITEM_B"
},
{
"_id": 2,
"buy": "BUY_2",
"sell": "SELL_2",
"item": "ITEM_G"
}
],
"filtereditems": [
{
"_id": 2,
"buy": "BUY_2",
"sell": "SELL_2",
"item": "ITEM_G",
"percentage": "55"
}
]
}
实际上,我想比较 percentages 文档与 discardeditems 和 filtereditems文件。如果 percentages 文档中的 buy、sell 和 item 值等于 discardeditems 文档中的值,我想在 discardeditems 文档中添加 isdiscardeditem:true strong>百分比文档。
如果 filtereditems 中的 buy、sell 和 item 值等于 percentages 文档中的值filtereditems 文档中的百分比值大于 percentages 文档中的百分比值,我不想再显示此记录.
我想看的数据最终版本应该是这样的;
{
"_id": 1,
"base": "A",
"buy": "BUY_1",
"sell": "SELL_1",
"item": "ITEM_B",
"ask": 100,
"bid": 114,
"percentage": 14,
"isdiscarded": true
},
{
"_id": 2,
"base": "B",
"buy": "BUY_2",
"sell": "SELL_2",
"item": "ITEM_G",
"ask": 50,
"bid": 90,
"percentage": 80,
"isdiscarded": true
}
百分比文档计数为 3。但现在我想展示两条记录。 percentages 文档的其他记录一定不能来,因为 filtereditems 中的百分比小于百分比。
我可以使用$lookup, $match $addFields 关键字添加isdiscardeditem,但我没有成功显示两条记录。 https://mongoplayground.net/p/_XZoqGTnIyz
我该怎么写?
【问题讨论】:
标签: mongodb mongoose mongodb-query aggregation-framework