2016-08-09 47 views
4
{ 
    "_id" : ObjectId("56bd8e9de517259412a743ab"), 
    "user_token" : "mzXhdbCu", 
    "sender_details" : { 
     "location" : "XYZ", 
     "zipcode" : "610208" 
    }, 
    "shipping_address" : { 
     "location" : "ABC", 
     "zipcode" : "602578 
    } 
} 

Ich habe die Anzahl der Instanzen jeder eindeutigen zipcode von beidenWie aus zwei Feldern in mongoDB zählen

$sender_details.zipcode 

und

$shipping_address.zipcode 

zu zählen versucht, habe ich versucht, das zu verwenden, folgender Code

db.ac_consignments.aggregate({ 
    $group: { 
     _id: { 
      "zipcode":"$sender_details.zipcode", 
      "szipcode":"$shipping_address.zipcode" 
     }, 
     count: {"$sum":1} 
    } 
}) 

Der Ausgang I erhalten ist diese

{ 
    "result" : [ 
     { 
      "_id" : { 
       "zipcode" : "610208", 
       "szipcode" : "602578" 
      }, 
      "count" : 7 
     }, 
     { 
      "_id" : { 
       "zipcode" : "602578", 
       "szipcode" : "678705" 
      }, 
      "count" : 51 
     } 
    ], 
    "ok" : 1 
} 

Aber was benötige ich die Zählung jedes zipcode in $sender_details.zipcode und $shipping_address.zipcode völlig. So eine Ausgabe wie diese

{ 
    "result" : [ 
     { 
      "_id" : { 
       "zipcode" : "610208", 
      }, 
      "count" : 7 
     }, 
     { 
      "_id" : { 
       "zipcode" : "602578" 
      }, 
      "count" : 51 
     } 
     { 
      "_id" : { 
       "zipcode" : "678705" 
      }, 
      "count" : 51 
     } 
    ], 
    "ok" : 1 
} 

Antwort

4

Die folgende Pipeline sollte für Sie arbeiten

db.getCollection('ac_consignments').aggregate([ 
    {  
     $project: { 
      zipcode: [ "$sender_details.zipcode", "$shipping_address.zipcode" ] 
     } 
    }, 
    { 
     $unwind: "$zipcode" 
    }, 
    { 
     $group: { 
      _id: "$zipcode", 
      count: { $sum: 1 } 
     } 
    } 
]) 

die Ausgabe wie diese

/* 1 */ 
{ 
    "_id" : "610208", 
    "count" : 1.0 
} 

/* 2 */ 
{ 
    "_id" : "610209", 
    "count" : 2.0 
} 

/* 3 */ 
{ 
    "_id" : "602578", 
    "count" : 1.0 
} 

/* 4 */ 
{ 
    "_id" : "602579", 
    "count" : 2.0 
} 

erzeugt, wenn die Verwendung von folgenden als Beispieldaten

/* 1 */ 
{ 
    "_id" : ObjectId("56bd8e9de517259412a743ab"), 
    "user_token" : "mzXhdbCu", 
    "sender_details" : { 
     "location" : "XYZ", 
     "zipcode" : "610208" 
    }, 
    "shipping_address" : { 
     "location" : "ABC", 
     "zipcode" : "602578" 
    } 
} 

/* 2 */ 
{ 
    "_id" : ObjectId("56bd8e9de517259412a743ac"), 
    "user_token" : "mzXhdbCu", 
    "sender_details" : { 
     "location" : "XYZ", 
     "zipcode" : "610209" 
    }, 
    "shipping_address" : { 
     "location" : "ABC", 
     "zipcode" : "602579" 
    } 
} 

/* 3 */ 
{ 
    "_id" : ObjectId("56bd8e9de517259412a753ac"), 
    "user_token" : "mzXhdbCu", 
    "sender_details" : { 
     "location" : "XYZ", 
     "zipcode" : "610209" 
    }, 
    "shipping_address" : { 
     "location" : "ABC", 
     "zipcode" : "602579" 
    } 
} 

finden Sie im folgenden GIF

GIF showing pipeline in action


Update für ältere Versionen

db.getCollection('ac_consignments').aggregate([ 
    { 
     $project: { 
      sender_zip: "$sender_details.zipcode", 
      shipping_zip: "$shipping_address.zipcode", 
      party: { $literal: ["sender_zip", "shipping_zip"] } 
     } 
    }, 
    { 
     $unwind: "$party" 
    }, 
    { 
     $group: { 
      _id: "$_id", 
      zipcode: { 
       $push: { 
        $cond: [ 
         { $eq: ["$party", "sender_zip"] }, 
         "$sender_zip", 
         "$shipping_zip" 
        ] 
       } 
      } 
     } 
    }, 
    { 
     $unwind: "$zipcode" 
    }, 
    { 
     $group: { 
      _id: "$zipcode", 
      count: { $sum: 1 } 
     } 
    } 
]) 
+0

scheint ein Fehler zu sein, wenn ich versuche, den Code auszuführen: Fehler ("Drucken Stapel Trace ") @: 0 () @ src/mongo/shell/utils.js: 37 ([Objekt Array]) @ src/mongo/shell/collection.js: 866 @ (Shell): 15 abgefangene Ausnahme: aggregate fehlgeschlagen: { \t "errmsg": "Ausnahme: Unzulässig Feldart Array in Objektausdruck (at 'zipcode')", \t "Code": 15992, \t "ok": 0 } –

+0

Welche Version von mongoDB verwenden Sie, da das Projizieren in ein Array neu in Version 3.2 ist, die meine Pipeline benötigt – DAXaholic

+0

mongoDB Version ist 2.0.4 –