需要帮助加入收藏

时间:2017-02-16 07:37:27

标签: mongodb

收集库存样本数据:

{
    "_id" : "89011704252315531324",
    "sku" : "A2015-01-000",
    "type" : "package",
    "status" : "active",
    "lng" : "-72.789153",
    "lat" : "44.173515",
    "acq" : "28",
    "gtime" : ISODate("2017-01-11T22:27:48.000Z"),
    "qlng" : "-72.796501",
    "qlat" : "44.214783",
    "qtime" : ISODate("2016-11-27T18:21:10.000Z"),
    "timestamp" : ISODate("2017-01-12T14:43:29.000Z"),
    "modified" : Date(-62135596800000),
    "battery" : "60",
    "wearables" : [ 
        {
            "_id" : "0009003C100228234E45",
            "type" : "wearable",
            "status" : "active",
            "battery" : "50",
            "timestamp" : ISODate("2017-01-12T11:43:33.000Z")
        }, 
        {
            "_id" : "004A003F200B36634E45",
            "type" : "cradle",
            "status" : "active",
            "battery" : "64",
            "timestamp" : ISODate("2017-01-11T22:27:26.000Z")
        }, 
        {
            "_id" : "11223344556600000B55",
            "type" : "falldetect",
            "status" : "active",
            "battery" : "64",
            "timestamp" : ISODate("2017-01-12T08:43:29.000Z")
        }
    ],
    "company" : "ConnectAmericaProduction",
    "companies" : [],
    "remoteIp" : "172.31.45.196:53864",
    "subscriber" : "5783e20aa2c89f346e000006",
    "ring" : "90",
    "speaker" : "90",
    "mic" : "55",
    "version" : "4352",
    "cradle" : "OFF",
    "ctime" : ISODate("2017-01-11T23:13:59.000Z"),
    "csqtime" : Date(-62135596800000)
}

收集calllog样本数据

{
    "_id" : "89011704252315531324",
    "cdr" : [ 
        {
            "direction" : "Outgoing",
            "duration" : 46,
            "timestamp" : ISODate("2016-11-23T03:25:06.000Z"),
            "number" : "",
            "name" : "Call Center",
            "lng" : "-71.208061",
            "lat" : "42.330265",
            "acq" : "",
            "timezone" : {
                "dstOffset" : 0.0,
                "rawOffset" : 0.0,
                "status" : "",
                "timeZoneId" : "",
                "timeZoneName" : ""
            }
        }, 
        {
            "direction" : "Incoming",
            "duration" : 51,
            "timestamp" : ISODate("2016-11-23T03:26:02.000Z"),
            "number" : "",
            "name" : "Call Center",
            "lng" : "-71.205727",
            "lat" : "42.333347",
            "acq" : "",
            "timezone" : {
                "dstOffset" : 0.0,
                "rawOffset" : 0.0,
                "status" : "",
                "timeZoneId" : "",
                "timeZoneName" : ""
            }
        }, 
        {
            "direction" : "Outgoing",
            "duration" : 49,
            "timestamp" : ISODate("2016-11-27T18:21:04.000Z"),
            "number" : "",
            "name" : "Call Center",
            "lng" : "-72.796501",
            "lat" : "44.214783",
            "acq" : "",
            "timezone" : {
                "dstOffset" : 0.0,
                "rawOffset" : -18000.0,
                "status" : "OK",
                "timeZoneId" : "America/New_York",
                "timeZoneName" : "Eastern Standard Time"
            }
        }
    ]
}
运行此aggrgrate函数后

 db.calllog.aggregate([{$unwind: "$cdr"}, {$lookup:{from: "inventory", localField: "_id", foreignField: "_id", as: "wearables" }}, { "$project": { "cdr.direction": 1, "cdr.duration": 1,"cdr.date": 1,"wearables.type": 1, "wearables.status": 1, "wearables.battery": 1} }])

结果:

{ "_id" : "89011704252315531324", "cdr" : { "direction" : "Outgoing", "duration" : 46 }, "wearables" : [ { "type" : "package", "status" : "active", "battery" : "60" } ] }
{ "_id" : "89011704252315531324", "cdr" : { "direction" : "Incoming", "duration" : 51 }, "wearables" : [ { "type" : "package", "status" : "active", "battery" : "60" } ] }
{ "_id" : "89011704252315531324", "cdr" : { "direction" : "Outgoing", "duration" : 49 }, "wearables" : [ { "type" : "package", "status" : "active", "battery" : "60" } ] }

所需的帮助无法获取查询以显示可穿戴设备类型,如可穿戴设备,摇篮,跌倒检测

感谢

1 个答案:

答案 0 :(得分:0)

你试过wearables.wearables.type吗?当您调用wearables.type时,您实际上正在获取Inventory的类型。如果您需要清单内的可穿戴设备的类型。你需要把inventory.wearables.type。最初的问题是您正在调用库存"可穿戴设备"并使其混淆。

我会做以下事情:

db.calllog.aggregate([{$unwind: "$cdr"},
      {$lookup:{from: "inventory", localField: "_id", foreignField: "_id", as: "inventory" }},
      {$unwind: "$inventory.wearables"},
      { "$project": {
          "cdr.direction": 1,
          "cdr.duration": 1,
          "cdr.date": 1,
          "inventory.type": 1,
          "inventory.status": 1, 
          "inventory.battery": 1,
          "inventory.wearables.type":1
      }}])
相关问题