2016-11-27 52 views
0

我的NodeJS - 蒙戈设置与DB,其配置如下(这里只显示了一个条目)如何进行后续蒙戈电话呼叫成功后

{ 
"filename":"type1.json","secs":72.4060092977,"platform":"mac","version":"1.3.0", "inputfile":"temp.mov" 
}, 

这里是我试图复制

蒙戈命令

db.perfR.distinct("platform") *(输出:[ “MAC”, “赢”])*

db.perfR.distinct("version",{"platform":"win"}) *(输出:[ “1.3.0”, “1.3.2”])*

db.perfR.find({"version":1.3.2,"platform":"win"},{"filename":1,"secs":1,"_id":0}) *(输出: “文件名”: “type1.json”, “秒”:72.4060092977])*

所以我试图做的是

for every platform 
for every version 
    get filename 
    get secs 

这是我写的代码迄今为止

function createPlatformDataSets(callback){     
var dbHost = "mongodb://mongo:27017/perfSample"; 
var mongodb = require('mongodb') 

var platformDataSets = [] 
var platformq = "platform" 
//get Instance of Mongoclient 
var MongoClient = mongodb.MongoClient; 

//Connecting to the Mongodb instance. 
//Make sure your mongodb daemon mongod is running on port 27017 on localhost 
MongoClient.connect(dbHost, function(err, db){ 
    if (err) throw err; 
    //use the distinct() to retrive distinct platforms 
    db.collection("perfR").distinct(platformq,function(err, platResultSet){ 
     if (err) throw err; 
     var maxPlatCnt = platResultSet.length // 1 
     if (maxPlatCnt == 0){ 
      console.log("Bad PlatfQ Query") 
      callback(true) 
     } 
     var versionedPlatDataSet = 0 
     for (p=0; p < platResultSet.length; p++){ 
      (function(index){ 
       var platform = platResultSet[index] 
       var options = createOptions(platform); 
       //Get Versions 
       var versionq = "\"version\",{\"platform\":" + platform + "}" 
       console.log("Versionq::"+versionq) 
       var dataSets = []; 
       //var versions = ["1.3.0", "1.3.2"]; // (select disctinct(version) from cpu where platform = plat) 
       // Use distinct() to find distinct Versions 
       db.collection("perfR").distinct(versionq,function(err, verResultSet){ 
        if (err) throw err; 
        var maxVerCnt = verResultSet.length // 2 
        if (maxVerCnt == 0){ 
         db.close() 
         console.log("Bad Versionq Query") 
         callback(true) 
         } 
        var dataSetResponseCnt = 0 
        for (v=0; v < verResultSet.length; v++){ 
          (function(idx){ 
           var dataq = "{platform:" + platform + ",version:" + version + "},{filename:1,secs:1,_id:0}" 
           // Use find() to find filename and secs for given version and platform 

         db.collection("perfR").find(dataq,function(err, dataResultSet){ 
            if (err) throw err; 
            if (dataResultSet.length == 0){ 
             console.log("Bad dataq Query") 
             callback(true) 
            } 
            //do something with filename and secs 
            dataSetResponseCnt++ 
            if (maxVerCnt == dataSetResponseCnt){ 
             var platformData = {"options":options, "labels":labels, "datasets":dataSets, "platform":platform} 
             platformDataSets.push(platformData) 
             if (versionedPlatDataSet == maxPlatCnt){ 
              db.close() 
              callback(null,platformDataSets) 
             } 
            } 
           }) 
          })(v) 
        } 
        versionedPlatDataSet++ 
      })(p) 
     } 
    } 
} 

在“1”,我能以检索截然不同的平台

但在“2”,我得到verResultSet.length为零。

有人可以指我什么是错的?

(PS:这是我第一次严重的问题,异步的JavaScript与我的代码,以便承担所有的建议都是欢迎的。)

回答

0

您使用的.distinct()是不正确的。当API实际采用两个单独的参数(不包括回调)时,您正在传递类似JSON的字符串。所以真正的代码其实应该只是或多或少您最初表明:

var query = { platform: 'win' }; 
db.collection('perfR').distinct('version', query, function(err, verResultSet) { 
    // ... 
}); 
1

可以使用Promises。所以例如你的代码将会是这样的:

return loadPlatforms().then(function (res){ 
    res.map(function(platform){ 
     loadVersion(platform).then(...) 
    } 
}) 
+0

感谢您的提示,这看起来很有前途。它与async.js有什么不同?我还没有使用async.js,但这是第一个在google上引发的链接。 – katch

+0

终于有了这个工作。虽然我很想消除一些更加骇人的东西。这是新代码https://gist.github.com/mrudulp/0bcda106534a407561fb6291edd26aff。我想消除这个'if(maxVerCnt == versionDataProcessed){'block。有一个更好的方法吗 – katch