2016-06-10 15 views
1

Ich arbeite an sailsjs Projekt, ich suche nur nach Vorschlag, um die unten Ausgabe zu erreichen, um die beste Leistung mit Code-Samples zu machen.Sailsjs native mit Mapreduce

Meine bestehende Sammlung mit diesem Dokument unten.

[{ 
    "word" : "DAD", 
    "createdAt":"6/10/2016 7:25:59 AM", 
"gamescore":1 
}, 
{ 
"word" : "SAD", 
"createdAt":"6/09/2016 7:25:59 AM", 
"gamescore":1 
}, 
{ 
    "word" : "PAD", 
     "createdAt":"6/10/2016 8:25:59 AM", 
    "gamescore":1 
}] 

Ich brauche den folgenden Ausgang, der so etwas ist.

[{ 
    "word" : "A", 
    "repeatedTimes" : "3", 
    "LatestRepeatedTime": "6/10/2016 8:25:59 AM" 
    }, 
{ 
     "word" : "D", 
    "repeatedTimes" : "4", 
    "LatestRepeatedTime": "6/10/2016 8:25:59 AM" 
    }, 
{ 
    "word" : "P", 
    "repeatedTimes" : "1", 
    "LatestRepeatedTime": "6/10/2016 8:25:59 AM" 
    }, 
    { 
    "word" : "S", 
     "repeatedTimes" : "1", 
     "LatestRepeatedTime": "6/09/2016 8:25:59 AM" 
    }] 

Für das obige Szenario implementiert i den Code unten zu holen, aber es ist nicht zu finden Abfrage arbeiten.

var m = function() { 
     var words = this.word; 
     if (words) { 
      for (var i = 0; i < words.length; i++) { 
       emit(words[i], 1); 
      } 
     } 
    } 

    var r = function (key, values) { 
     var count = 0; 
     values.forEach(function (v) { 
      count += v; 
     }); 
     return count; 
    } 
    console.log(req.params.childid); 
    Activity.native(function (err, collection) { 
     console.log("hello"); 
     collection.mapReduce(m, r, { 
      out: {merge: "words_count" + "_" + "575a4952bfb2ad01481e9060"} 
     }, function (err, result) { 
      Activity.getDB(function (err, db) { 
      var colname = "words_count" + "_" + "575a4952bfb2ad01481e9060"; 
      var natCol = db.collection('words_count' + "_" + "575a4952bfb2ad01481e9060"); 

       natCol.find({},..... **is not working** 

       natCol.count({}, function (err, docs) { 
        console.log(err); 
        console.log(docs); 
        res.ok(docs); 
       }); 
      }); 
     }); 
    }); 

Antwort:

natCol.aggregate([ 
       { 
        $project: 
        { 
         _id: "$_id" , 
         value:"$value"       
        }  
       } 
       ], function(err, data){ 
        console.log(data); 
        res.ok(data); 
       }); 

Antwort

1

Sie könnten versuchen, den folgenden

var m = function() {   
     if (this.word) { 
      for (var i = 0; i < this.word.length; i++) { 
       emit(this.word[i], { 
        "repeatedTimes": 1, 
        "LatestRepeatedTime": this.createdAt 
       }); 
      } 
     } 
    }; 

var r = function (key, values) { 
    var obj = {}; 
    values.forEach(function(value) { 
     printjson(value); 
     Object.keys(value).forEach(function(key) { 
      if (!obj.hasOwnProperty(key)) obj[key] = 0; 
      if (key === "repeatedTimes") obj[key] += value[key]; 
     }); 
     obj["LatestRepeatedTime"] = value["LatestRepeatedTime"]; 
    }); 
    return obj; 
}; 

var opts = { out: {inline: 1} }; 

Activity.native(function (err, collection) { 
    collection.mapReduce(m, r, opts, function (err, result) { 
     console.log(err); 
     console.log(result); 
     res.ok(result); 
    }); 
}); 
+1

Das ist wirklich sauberen Code. awesomoe. Danke, Chridam. Auch ich bekomme diese Karte und reduziere die gleiche, wie Sie es mit einigen clumpsy im nativen Gebrauch in sailsjs zur Verfügung gestellt haben. Ich habe meinen Code mit der obigen Antwort gereinigt. Danke @chridam. –