$ match里面$不合计

时间:2017-08-14 05:37:13

标签: arrays node.js mongodb mongoose aggregation-framework

我试图获取与serviceLocationId id字段集合匹配的所有订单。

订单收集:

{
    "_id" : ObjectId("598b0560f514e531713b24b0"),
    "orderId" : ObjectId("598b055ff514e531713b24af"),
    "orderNo" : "AQ144O1701011",
    "serviceLocationId" : "5976e2cce05d17132a343975",
    "jobPriority" : "8",
    "description" : "Spinneys Mercato",
    "serviceType" : "pickup",
    "orderSource" : "import",
    "takenBy" : "Rany",
    "plannedDeliveryDate" : ISODate("2017-08-13T00:00:00Z"),
    "actualDeliveryDate" : ISODate("2017-10-13T00:00:00Z"),
    "deliveredBy" : "Hamid",
    "lineItems" : [
        {
            "itemId" : "Item09",
            "orderQty" : 154,
            "deliveredQty" : 154,
            "status" : "UNASSIGNED",
            "size1" : "100",
            "size2" : "200",
            "size3" : "300",
            "_id" : ObjectId("598b0560f514e531713b24b1")
        }
    ],
    "actualDeliveryTime" : {
        "from" : "10PM",
        "to" : "7PM"
    },
    "plannedDeliveryTime" : {
        "from" : "12PM",
        "to" : "11PM"
    },
    "orderDate" : ISODate("2017-08-09T12:51:44Z"),
    "__v" : 0
}

servicelocations collection:

{
    "_id" : ObjectId("5976e2cce05d17132a343976"),
    "regionId" : ObjectId("5975e82dd857e0444013f426"),
    "zoneId" : ObjectId("5975eba0a392a045d77d1b12"),
    "description" : "Spinneys Mercato",
    "locationPriority" : 1,
    "accountType" : "CHAIN CUSTOMER",
    "locationType" : "Service Location",
    "address1" : "Spinneys Mercato",
    "address2" : "Marcatto Centre, Beach Road",
    "phone" : "123456",
    "city" : "Jumeirah",
    "county" : null,
    "zipcode" : null,
    "state" : null,
    "country" : "United Arab Emirates",
    "imageUrl" : "",
    "deliveryDays" : "1",
    "status" : "Delivered",
    "createdBy" : "Hitesh",
    "updatedBy" : null,
    "serviceLocationId" : ObjectId("5976e2cce05d17132a343975"),
    "updateDate" : null,
    "createdDate" : ISODate("2017-07-25T06:18:52.007Z"),
    "location" : {
        "makani" : null,
        "geo" : {
            "latitude" : "25.216547",
            "longitude" : "55.253636"
        }
    },
    "contact" : {
        "name" : "shiv",
        "phone" : "1111111111",
        "email" : "test1@gmail.com"
    },
    "serviceTime" : {
        "serviceTimeType" : "598ad8718db899b9d4ff2bc1",
        "nonhelper" : {
            "fixedTime" : "10",
            "variableTime" : "25"
        },
        "helper" : {
            "fixedTime" : "10",
            "variableTime" : "15"
        }
    },
    "timeWindow" : [
        {
            "timeWindowType" : "598ad7578db899b9d4ff2bb9",
            "start" : "",
            "close" : "",
            "_id" : ObjectId("5976e2cce05d17132a343977")
        }
    ],
    "__v" : 0
}

查询:

db.collection.aggregate([
        {   $match: { 'serviceLocationId' : { $in: [ObjectId("5976e2cce05d17132a343975")]}  }  } ,
        {
            $lookup:
            {
                from:'orders',
                localField:'serviceLocationId',
                foreignField:'serviceLocationId',
                as:'orders'
            }
        }
    ])

响应:

{
    "_id" : ObjectId("598592dc8db31d6625a412aa"),
    "regionId" : ObjectId("5975e82dd857e0444013f426"),
    "zoneId" : ObjectId("5975eb8ba392a045d77d1b10"),
    "description" : "ASWAAQ- AL BADAA",
    "locationPriority" : 7,
    "locationType" : "Service Location",
    "address1" : "ASWAAQ- AL BADAA",
    "address2" : "AL BADAA, AL WASL ROAD.DUBAI",
    "phone" : "123456",
    "city" : null,
    "county" : null,
    "zipcode" : null,
    "state" : null,
    "country" : "United Arab Emirates",
    "imageUrl" : "",
    "deliveryDays" : "1",
    "status" : "Delivered",
    "createdBy" : "Hitesh",
    "updatedBy" : null,
    "serviceLocationId" : ObjectId("598592dc8db31d6625a41290"),
    "accountType" : "CREDIT GROCERY",
    "updateDate" : null,
    "createdDate" : ISODate("2017-07-25T06:20:15.519Z"),
    "location" : {
        "makani" : null,
        "geo" : {
            "latitude" : "25.2037594",
            "longitude" : "55.2752419"
        }
    },
    "contact" : {
        "name" : "krishna",
        "phone" : "1111111119",
        "email" : "test9@gmail.hypermarketsm"
    },
    "serviceTime" : {
        "serviceTimeType" : "598ad89e8db899b9d4ff2bc5",
        "nonhelper" : {
            "fixedTime" : "10",
            "variableTime" : "25"
        },
        "helper" : {
            "fixedTime" : "10",
            "variableTime" : "15"
        }
    },
    "timeWindow" : [
        {
            "timeWindowType" : "598ad76d8db899b9d4ff2bbd",
            "start" : "",
            "close" : "",
            "_id" : ObjectId("5976e31fe05d17132a34397d")
        }
    ],
    "__v" : 0,
    "orders" : [ ]
}

1 个答案:

答案 0 :(得分:0)

我在servicelocation集合中将servicelocationId更改为字符串,现在它正常工作。

"serviceLocationId" : ObjectId("598592dc8db31d6625a41290") "serviceLocationId" : "598592dc8db31d6625a41290"

<强>查询:

db.getCollection('servicelocations').aggregate([
        {   $match: { 'serviceLocationId' : { $in: ["5976e2cce05d17132a343975"]}}} ,
        {
            $lookup:
            {
                from:'orders',
                localField:'serviceLocationId',
                foreignField:'serviceLocationId',
                as:'orders'
            }
        }
    ])

<强>溶液

/ * 1 * /

{
    "_id" : ObjectId("5976e2cce05d17132a343976"),
    "regionId" : ObjectId("5975e82dd857e0444013f426"),
    "zoneId" : ObjectId("5975eba0a392a045d77d1b12"),
    "description" : "Spinneys Mercato",
    "locationPriority" : 1,
    "accountType" : "CHAIN CUSTOMER",
    "locationType" : "Service Location",
    "address1" : "Spinneys Mercato",
    "address2" : "Marcatto Centre, Beach Road",
    "phone" : "123456",
    "city" : "Jumeirah",
    "county" : null,
    "zipcode" : null,
    "state" : null,
    "country" : "United Arab Emirates",
    "imageUrl" : "",
    "imageUrl" : "",
    "deliveryDays" : "1",
    "status" : "Delivered",
    "createdBy" : "Hitesh",
    "updatedBy" : null,
    "serviceLocationId" : "5976e2cce05d17132a343975",
    "updateDate" : null,
    "createdDate" : ISODate("2017-07-25T06:18:52.007Z"),
    "location" : {
        "makani" : null,
        "geo" : {
            "latitude" : "25.216547",
            "longitude" : "55.253636"
        }
    },
    "contact" : {
        "name" : "shiv",
        "phone" : "1111111111",
        "email" : "test1@gmail.com"
    },
    "serviceTime" : {
        "serviceTimeType" : "598ad8718db899b9d4ff2bc1",
        "nonhelper" : {
            "fixedTime" : "10",
            "variableTime" : "25"
        },
        "helper" : {
            "fixedTime" : "10",
            "variableTime" : "15"
        }
    },
    "timeWindow" : [ 
        {
            "timeWindowType" : "598ad7578db899b9d4ff2bb9",
            "start" : "",
            "close" : "",
            "_id" : ObjectId("5976e2cce05d17132a343977")
        }
    ],
    "__v" : 0,
    "orders" : [ 
        {
            "_id" : ObjectId("598b0560f514e531713b24b0"),
            "orderId" : ObjectId("598b055ff514e531713b24af"),
            "orderNo" : "AQ144O1701011",
            "serviceLocationId" : "5976e2cce05d17132a343975",
            "jobPriority" : "8",
            "description" : "Spinneys Mercato",
            "serviceType" : "pickup",
            "orderSource" : "import",
            "takenBy" : "Rany",
            "plannedDeliveryDate" : ISODate("2017-08-13T00:00:00.000Z"),
            "actualDeliveryDate" : ISODate("2017-10-13T00:00:00.000Z"),
            "deliveredBy" : "Hamid",
            "lineItems" : [ 
                {
                    "itemId" : "Item09",
                    "orderQty" : 154,
                    "deliveredQty" : 154,
                    "status" : "UNASSIGNED",
                    "size1" : "100",
                    "size2" : "200",
                    "size3" : "300",
                    "_id" : ObjectId("598b0560f514e531713b24b1")
                }
            ],
            "actualDeliveryTime" : {
                "from" : "10PM",
                "to" : "7PM"
            },
            "plannedDeliveryTime" : {
                "from" : "12PM",
                "to" : "11PM"
            },
            "orderDate" : ISODate("2017-08-09T12:51:44.000Z"),
            "__v" : 0
        }, 
        {
            "_id" : ObjectId("598c3d9880c4d4366cf3e35d"),
            "orderId" : ObjectId("598c3d9880c4d4366cf3e35c"),
            "orderNo" : "AQ144O1701011",
            "serviceLocationId" : "5976e2cce05d17132a343975",
            "jobPriority" : "1",
            "description" : "Spinneys Mercato",
            "serviceType" : "pickup",
            "orderSource" : "import",
            "takenBy" : "Adam",
            "plannedDeliveryDate" : ISODate("2017-09-06T18:30:00.000Z"),
            "actualDeliveryDate" : ISODate("2017-10-11T00:00:00.000Z"),
            "deliveredBy" : "Adam",
            "lineItems" : [ 
                {
                    "itemId" : "Item09",
                    "orderQty" : 15,
                    "deliveredQty" : 15,
                    "status" : "ASSIGNED",
                    "_id" : ObjectId("598c3d9880c4d4366cf3e35e")
                }
            ],
            "actualDeliveryTime" : {
                "from" : "11PM",
                "to" : "12PM"
            },
            "plannedDeliveryTime" : {
                "from" : "10PM",
                "to" : "10PM"
            },
            "orderDate" : ISODate("2017-08-10T11:03:52.170Z"),
            "__v" : 0
        }
    ]
}