【问题标题】:How to get 2nd highest status in mongodb with a collection having multiple status如何通过具有多个状态的集合在 mongodb 中获得第二高状态
【发布时间】:2021-01-24 04:12:23
【问题描述】:
    /* 1 */
    {
        "_id" : ObjectId("5f0b6699486ea60a2a9c62fh"),
        "EventDatetime" : "2020-07-12T19:38:00.653",
        "Eventstatus" : "05",
        "DelNumber" : "0703676929",
        "DELIVERY_ITEM" : [ 
            {
                "Product_Code" : "123",
                "Product_Name" : "utensils",
                "Shipment_Quantity" : "12",
            }, 
            {
                "Product_Code" : "456",
                "Product_Name" : "clothes",
                "Shipment_Quantity" : "10",
            }
        ]
    }
    {
        "_id" : ObjectId("5f0b6699486ea60a2a9c62fi"),
        "EventDatetime" : "2020-07-12T19:38:00.653",
        "Eventstatus" : "03",
        "DelNumber" : "0703676929",,
        "DELIVERY_ITEM" : [ 
            {
                "Product_Code" : "123",
                "Product_Name" : "utensils",
                "Shipment_Quantity" : "12",
            }, 
            {
                "Product_Code" : "456",
                "Product_Name" : "clothes",
                "Shipment_Quantity" : "10",
            }
        ]
    }
    {
        "_id" : ObjectId("5f0b6699486ea60a2a9c62fg"),
        "EventDatetime" : "2020-07-12T19:38:00.653",
        "Eventstatus" : "02",
        "DelNumber" : "0703676929",
        "DELIVERY_ITEM" : [ 
            {
                "Product_Code" : "123",
                "Product_Name" : "utensils",
                "Shipment_Quantity" : "12",
            }, 
            {
                "Product_Code" : "456",
                "Product_Name" : "clothes",
                "Shipment_Quantity" : "10",
            }
        ]
    }
    /*2*/

    {
        "_id" : ObjectId("5f0b6699486ea60a2a9c62fa"),
        "EventDatetime" : "2020-07-12T19:38:00.653",
        "Eventstatus" : "04",
        "DelNumber" : "0703676928",
        "DELIVERY_ITEM" : [ 
            {
                "Product_Code" : "123",
                "Product_Name" : "utensils",
                "Shipment_Quantity" : "12",
            }, 
            {
                "Product_Code" : "456",
                "Product_Name" : "clothes",
                "Shipment_Quantity" : "10",
            }
        ]
    }
    {
        "_id" : ObjectId("5f0b6699486ea60a2a9c62fb"),
        "EventDatetime" : "2020-07-12T19:38:00.653",
        "Eventstatus" : "02",
        "DelNumber" : "0703676928",
        "DELIVERY_ITEM" : [ 
            {
                "Product_Code" : "123",
                "Product_Name" : "utensils",
                "Shipment_Quantity" : "12",
            }, 
            {
                "Product_Code" : "456",
                "Product_Name" : "clothes",
                "Shipment_Quantity" : "10",
            }
        ]
    }
    {
        "_id" : ObjectId("5f0b6699486ea60a2a9c62fc"),
        "EventDatetime" : "2020-07-12T19:38:00.653",
        "Eventstatus" : "01",
        "DelNumber" : "0703676928",
        "DELIVERY_ITEM" : [ 
            {
                "Product_Code" : "123",
                "Product_Name" : "utensils",
                "Shipment_Quantity" : "12",
            }, 
            {
                "Product_Code" : "456",
                "Product_Name" : "clothes",
                "Shipment_Quantity" : "10",
            }
        ]
    }

我正在收集订单。将其视为电子商务网站。现在,订单可以有多个状态 00,01,02,03,04 和 05 。 05是指订单的实时跟踪。所以我的 delno.1 可以有状态 01,02,05,05,03,05,05。我的问题是如何获得我运行的查询将 03 作为最新状态而不是 05。 此外,当状态达到 04 时,我不需要检索该记录。 任何帮助都会很棒!

【问题讨论】:

  • 您可以在问题中添加您的订单记录吗?
  • 在订购文件后使用 $slice(2) 进行 $push。

标签: mongodb aggregation-framework query-optimization


【解决方案1】:

您必须首先删除所有状态为 04 的文档。然后对同一集合进行查找以获取剩余的文档。展开它并过滤掉状态为 05 的文档。

[
  {
    //Group records by DelNumber
    $group: {
      _id: '$DelNumber',
      EventStatuses: {
        $addToSet: '$Eventstatus'
      }
    }
  }, {
    // Remove any with status 04
    $match: {
      EventStatuses: {
        $ne: '04'
      }
    }
  }, {
    // Looksup on same collection to get full document
    $lookup: {
      from: 'collection',
      localField: '_id',
      foreignField: 'DelNumber',
      as: 'matched'
    }
  }, {
    // Unwind the matched records
    $unwind: {
      path: '$matched'
    }
  }, {
    $replaceRoot: {
      newRoot: '$matched'
    }
  }, {
    // Remove any record with status 05
    $match: {
      Eventstatus: {
        $ne: '05'
      }
    }
  }
]

【讨论】:

    【解决方案2】:

    你可以试试,

    • $match 你的条件,DelNumber 是“0703676929”,Eventstatus 不等于第 4 个
    • $addFields 使用 $toInt 在整数字段中添加 Eventstatus
    • $sort 按状态字段降序排列
    • $skip 在第二页 (1) 因为第一个是 5 状态
    • $limit单条记录
    db.collection.aggregate([
      { 
        $match: { 
          DelNumber: "0703676929",
          Eventstatus: { $ne: "04" } 
        } 
      }, 
      { $addFields: { status: { $toInt: "$Eventstatus" } } },
      { $sort: { status: -1 } },
      { $skip: 1 },
      { $limit: 1 }
    ])
    

    Playground


    第二种方式,您可以在第 5 状态下使用修复条件,

    • $match 你的条件,DelNumber 是“0703676929”,Eventstatus 不等于 4th 和 5th
    • $addFields 使用 $toInt 在整数字段中添加 Eventstatus
    • $sort 按状态字段降序排列
    • $limit单条记录
    db.collection.aggregate([
      {
        $match: {
          DelNumber: "0703676929",
          Eventstatus: { $nin: ["05", "04"] }
        }
      },
      { $addFields: { status: { $toInt: "$Eventstatus" } } },
      { $sort: { status: -1 } },
      { $limit: 1 }
    ])
    

    Playground

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多