2017-02-16 6 views
-1

Sammlung Inventarbeispieldaten:Benötigen Sie Hilfe Sammlung Join

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

Sammlung calllog Beispieldaten

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

nach Durchlauf dieser aggrgrate Funktion

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

Ergebnis:

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

benötigte Hilfe nicht Abfrage zeigt Wearables-Typ, wie tragbar, Wiege bekommen, falldetect

danken

Antwort

0

Haben Sie versucht, wearables.wearables.type zu tun? Wenn Sie wearables.type aufrufen, erhalten Sie tatsächlich den Typ des Inventars. Wenn Sie den Typ des Wearables benötigen, der sich im Inventar befindet. Sie müssen inventory.wearables.type eingeben. Das erste Problem ist, dass Sie das Inventar als "Wearables" bezeichnen und Verwirrung stiften.

Ich würde folgendes tun:

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

es läuft, zeigt aber keine Aufzeichnungen –

+0

Sie die Projektionen zu entfernen Versuchen? –

+0

Ich habe es in MongoDB laufen, aber wenn ich versuchte, dies in R zu übergeben, wenn Sie mir einen Fehler geben. –