mongo 子字段的范围查询,子字段的聚合查询

 

 

 

db.getCollection('文档名字').aggregate(
  [
    { 
      $match : { "POL_CODE" : "CNTAO","POD_CODE" : "USNYC" } 
    },
    {
      $project: {

      "POL_CODE":1,
      "POD_CODE":1,

         "ship_schedule": {
          $filter: {
            input: "$ship_schedule",
            as: "item",
            cond: {"$and":[ { $gt : ["$$item.ETD","2022-05-18"]},{ $lt : ["$$item.ETD","2022-05-22"]}] }
          }
        }
      }
    }
  ]
)

 

 

 

 

 

{
    "_id" : ObjectId("6xxxxca79"),
    "ship_schedule" : [ 
        {
            "CURRENT_COUNT" : 81,
            "ETA" : "2022-07-06",
            "ETD" : "2022-05-21",
            "IS_TRANSIT" : 1,
            "P2P_ID" : 5072,
            "POD_CODE" : "USNYC",
            "POD_NAME" : "NEW YORK, NY (USNYC)",
            "POL_CODE" : "CNTAO",
            "POL_NAME" : "QINGDAO (CNTAO)",
            "ROUTE_CODE" : "EC2",
            "TRANSIT_LIST" : [ 
                {
                    "TRANSIT_PORT_EN" : "PUSAN",
                    "TRANSIT_PORT_CODE" : null,
                    "TRANSIT_ROUTE_CODE" : "PN3",
                    "TRANSIT_VESSEL" : "SEASPAN THAMES",
                    "TRANSIT_VOYAGE" : "026E"
                }, 
                {
                    "TRANSIT_PORT_EN" : "SEATTLE",
                    "TRANSIT_PORT_CODE" : null,
                    "TRANSIT_ROUTE_CODE" : null,
                    "TRANSIT_VESSEL" : "RAIL",
                    "TRANSIT_VOYAGE" : null
                }, 
                {
                    "TRANSIT_PORT_EN" : "CHICAGO",
                    "TRANSIT_PORT_CODE" : null,
                    "TRANSIT_ROUTE_CODE" : null,
                    "TRANSIT_VESSEL" : "RAIL",
                    "TRANSIT_VOYAGE" : null
                }
            ],
            "TRANSIT_TIME" : "46",
            "VESSEL" : "ONE APUS",
            "VOYAGE" : "010E"
        }, 
        {
            "CURRENT_COUNT" : 45,
            "ETA" : "2022-06-25",
            "ETD" : "2022-05-21",
            "IS_TRANSIT" : 0,
            "P2P_ID" : 5072,
            "POD_CODE" : "USNYC",
            "POD_NAME" : "NEW YORK, NY (USNYC)",
            "POL_CODE" : "CNTAO",
            "POL_NAME" : "QINGDAO (CNTAO)",
            "ROUTE_CODE" : "EC2",
            "TRANSIT_LIST" : [],
            "TRANSIT_TIME" : "35",
            "VESSEL" : "ONE APUS",
            "VOYAGE" : "010E"
        }, 
        {
            "CURRENT_COUNT" : 30,
            "ETA" : "2022-06-21",
            "ETD" : "2022-05-20",
            "IS_TRANSIT" : 1,
            "P2P_ID" : 5072,
            "POD_CODE" : "USNYC",
            "POD_NAME" : "NEW YORK, NY (USNYC)",
            "POL_CODE" : "CNTAO",
            "POL_NAME" : "QINGDAO (CNTAO)",
            "ROUTE_CODE" : "CEN",
            "TRANSIT_LIST" : [ 
                {
                    "TRANSIT_PORT_EN" : "LONG BEACH",
                    "TRANSIT_PORT_CODE" : null,
                    "TRANSIT_ROUTE_CODE" : null,
                    "TRANSIT_VESSEL" : "TRUCK",
                    "TRANSIT_VOYAGE" : null
                }, 
                {
                    "TRANSIT_PORT_EN" : "LOS ANGELES",
                    "TRANSIT_PORT_CODE" : null,
                    "TRANSIT_ROUTE_CODE" : null,
                    "TRANSIT_VESSEL" : "RAIL",
                    "TRANSIT_VOYAGE" : null
                }, 
                {
                    "TRANSIT_PORT_EN" : "CHICAGO",
                    "TRANSIT_PORT_CODE" : null,
                    "TRANSIT_ROUTE_CODE" : null,
                    "TRANSIT_VESSEL" : "RAIL",
                    "TRANSIT_VOYAGE" : null
                }
            ],
            "TRANSIT_TIME" : "32",
            "VESSEL" : "CSCL AUTUMN",
            "VOYAGE" : "045E"
        }
    ]
}

 

posted @ 2022-05-18 17:02  AngDH  阅读(214)  评论(0编辑  收藏  举报