【问题标题】:mongodb aggregation compare documents and i don't want to show incompatible onesmongodb聚合比较文档,我不想显示不兼容的
【发布时间】: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


    【解决方案1】:

    您可以尝试以下聚合:

    db.percentages.aggregate([
        {
            $lookup: {
                from: "discardeditems",
                let: {
                    item_src: "$item",
                    buy_src: "$buy",
                    sell_src: "$sell"
                },
                pipeline: [
                    {
                        $match: {
                            $expr: {
                                $and: [
                                    { $eq: [ "$$item_src", "$item" ] },
                                    { $eq: [ "$$buy_src", "$buy" ] },
                                    { $eq: [ "$$sell_src", "$sell" ] },
                                ]
                            }
                        }
                    }
                ],
                as: "discarded"
            }
        },
        {
            $lookup: {
                from: "filtereditems",
                let: {
                    item_src: "$item",
                    buy_src: "$buy",
                    sell_src: "$sell",
                    percentage_src: "$percentage"
                },
                pipeline: [
                    {
                        $match: {
                            $expr: {
                                $and: [
                                    { $eq: [ "$$item_src", "$item" ] },
                                    { $eq: [ "$$buy_src", "$buy" ] },
                                    { $eq: [ "$$sell_src", "$sell" ] },
                                    { $lt: [ "$$percentage_src", { $toInt: "$percentage" } ] }
                                ]
                            }
                        }
                    }
                ],
                as: "filtered"
            }
        },
        {
            $match: {
                filtered: { $eq: [] }
            }
        },
        {
            $addFields: {
                isdiscarded: { $gt: [ { $size: "$discarded" }, 0 ] }
            }
        },
        {
            $project: {
                discarded: 0,
                filtered: 0
            }
        }
    ])
    

    请注意percentage 字段必须具有相同的类型,因此需要$toInt 进行转换。

    Mongo Playground

    【讨论】:

    • 非常感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 2015-03-22
    • 2020-12-10
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多