2017-08-14 76 views
0

我试图获得所有与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集合:

{ 
    "_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" : "[email protected]" 
    }, 
    "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" : "[email protected]" 
    }, 
    "serviceTime" : { 
     "serviceTimeType" : "598ad89e8db899b9d4ff2bc5", 
     "nonhelper" : { 
      "fixedTime" : "10", 
      "variableTime" : "25" 
     }, 
     "helper" : { 
      "fixedTime" : "10", 
      "variableTime" : "15" 
     } 
    }, 
    "timeWindow" : [ 
     { 
      "timeWindowType" : "598ad76d8db899b9d4ff2bbd", 
      "start" : "", 
      "close" : "", 
      "_id" : ObjectId("5976e31fe05d17132a34397d") 
     } 
    ], 
    "__v" : 0, 
    "orders" : [ ] 
} 
+0

“不工作”是什么意思?没有结果?错误?请发布更多的细节1)您的MongoDB版本,2)示例文档,3)预期的结果 –

+0

@KevinAdistambha看到我的结果,我得到的订单字段为空。 –

+0

您还没有回答其他两个问题:1)您的MongoDB版本2)两个集合中的示例文档。 –

回答

0

我改变servicelocationId作为字符串的serviceLocation收集现在它的正常工作。

"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" : "[email protected]" 
    }, 
    "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 
     } 
    ] 
}