$lookup on ObjectId's in an array

后端 未结 6 1641
忘了有多久
忘了有多久 2020-11-22 04:53

What\'s the syntax for doing a $lookup on a field that is an array of ObjectIds rather than just a single ObjectId?

Example Order Document:

{
  _id:          


        
6条回答
  •  清酒与你
    2020-11-22 05:30

    use $unwind you will get the first object instead of array of objects

    query:

    db.getCollection('vehicles').aggregate([
      {
        $match: {
          status: "AVAILABLE",
          vehicleTypeId: {
            $in: Array.from(newSet(d.vehicleTypeIds))
          }
        }
      },
      {
        $lookup: {
          from: "servicelocations",
          localField: "locationId",
          foreignField: "serviceLocationId",
          as: "locations"
        }
      },
      {
        $unwind: "$locations"
      }
    ]);
    

    result:

    {
        "_id" : ObjectId("59c3983a647101ec58ddcf90"),
        "vehicleId" : "45680",
        "regionId" : 1.0,
        "vehicleTypeId" : "10TONBOX",
        "locationId" : "100",
        "description" : "Isuzu/2003-10 Ton/Box",
        "deviceId" : "",
        "earliestStart" : 36000.0,
        "latestArrival" : 54000.0,
        "status" : "AVAILABLE",
        "accountId" : 1.0,
        "locations" : {
            "_id" : ObjectId("59c3afeab7799c90ebb3291f"),
            "serviceLocationId" : "100",
            "regionId" : 1.0,
            "zoneId" : "DXBZONE1",
            "description" : "Masafi Park Al Quoz",
            "locationPriority" : 1.0,
            "accountTypeId" : 0.0,
            "locationType" : "DEPOT",
            "location" : {
                "makani" : "",
                "lat" : 25.123091,
                "lng" : 55.21082
            },
            "deliveryDays" : "MTWRFSU",
            "timeWindow" : {
                "timeWindowTypeId" : "1"
            },
            "address1" : "",
            "address2" : "",
            "phone" : "",
            "city" : "",
            "county" : "",
            "state" : "",
            "country" : "",
            "zipcode" : "",
            "imageUrl" : "",
            "contact" : {
                "name" : "",
                "email" : ""
            },
            "status" : "",
            "createdBy" : "",
            "updatedBy" : "",
            "updateDate" : "",
            "accountId" : 1.0,
            "serviceTimeTypeId" : "1"
        }
    }
    
    
    {
        "_id" : ObjectId("59c3983a647101ec58ddcf91"),
        "vehicleId" : "81765",
        "regionId" : 1.0,
        "vehicleTypeId" : "10TONBOX",
        "locationId" : "100",
        "description" : "Hino/2004-10 Ton/Box",
        "deviceId" : "",
        "earliestStart" : 36000.0,
        "latestArrival" : 54000.0,
        "status" : "AVAILABLE",
        "accountId" : 1.0,
        "locations" : {
            "_id" : ObjectId("59c3afeab7799c90ebb3291f"),
            "serviceLocationId" : "100",
            "regionId" : 1.0,
            "zoneId" : "DXBZONE1",
            "description" : "Masafi Park Al Quoz",
            "locationPriority" : 1.0,
            "accountTypeId" : 0.0,
            "locationType" : "DEPOT",
            "location" : {
                "makani" : "",
                "lat" : 25.123091,
                "lng" : 55.21082
            },
            "deliveryDays" : "MTWRFSU",
            "timeWindow" : {
                "timeWindowTypeId" : "1"
            },
            "address1" : "",
            "address2" : "",
            "phone" : "",
            "city" : "",
            "county" : "",
            "state" : "",
            "country" : "",
            "zipcode" : "",
            "imageUrl" : "",
            "contact" : {
                "name" : "",
                "email" : ""
            },
            "status" : "",
            "createdBy" : "",
            "updatedBy" : "",
            "updateDate" : "",
            "accountId" : 1.0,
            "serviceTimeTypeId" : "1"
        }
    }
    

提交回复
热议问题