为了账号安全,请及时绑定邮箱和手机立即绑定

数组中对象的$查找

数组中对象的$查找

阿波罗的战车 2019-06-25 15:06:16
数组中对象的$查找对于作为ObjectId数组而不仅仅是一个对象的字段执行$查找的语法是什么?示例订单文档:{   _id: ObjectId("..."),   products: [     ObjectId("..<Car ObjectId>.."),     ObjectId("..<Bike ObjectId>..")   ]}不工作查询:db.orders.aggregate([     {        $lookup:          {            from: "products",            localField: "products",            foreignField: "_id",            as: "productObjects"          }     }])期望结果{   _id: ObjectId("..."),   products: [     ObjectId("..<Car ObjectId>.."),     ObjectId("..<Bike ObjectId>..")   ],   productObjects: [     {<Car Object>},     {<Bike Object>}   ],}
查看完整描述

3 回答

?
慕姐4208626

TA贡献1852条经验 获得超7个赞

这个$lookup聚合管道阶段现在直接用于数组(3.3.4版本)。

见:在本地(多个)值数组和外部(单个)值之间查找


查看完整回答
反对 回复 2019-06-25
?
慕运维8079593

TA贡献1876条经验 获得超5个赞

使用美元您将得到第一个对象,而不是对象数组。

查询:

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"
  }]);

结果:

{
    "_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"
    }}


查看完整回答
反对 回复 2019-06-25
  • 3 回答
  • 0 关注
  • 644 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信