2017-02-16 94 views
-1

收集库存样本数据:需要帮助加入收藏

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

需要帮助不能得到查询显示身打扮类型,如耐磨,摇篮,falldetect

感谢

回答

0

你尝试做wearables.wearables.type?当您调用wearables.type时,您实际上正在获取库存的类型。如果您需要库存内的可穿戴设备的类型。你需要把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 
     }}]) 
+0

运行它,但显示没有记录 –

+0

你尝试删除的预测? –

+0

我知道它在MongoDB中运行,但是当我试图在R中通过这个,如果给我一个错误。 –