2016-06-10 4 views
1

나는 sailsjs 프로젝트에서 일하고있다. 나는 아래 코드를 사용하여 최상의 성능을 낼 수있는 제안을 찾고있다.Sailsjs Mapreduce 네이티브

내 기존 컬렉션에 아래 문서가 있습니다.

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

다음과 같은 출력이 필요합니다.

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

위의 시나리오에서 아래의 코드를 가져 와서 구현했지만 찾기 쿼리에서 작동하지 않습니다.

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

답변 :

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

답변

1

다음과 같은

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

이 정말 깨끗 코드를 시도 할 수 있습니다. awesomoe. 감사합니다 chridam. 나도 그지도를 얻고 sailsjs에서의 네이티브 사용법에서 약간의 덩어리를 제공하는 것과 같은 것을 줄인다. 나는 위의 대답으로 내 코드를 청소했다. @chridam 감사합니다. –