如何在成功通话后进行后续的mongo通话

时间:2016-11-27 07:19:53

标签: javascript node.js mongodb

我有nodejs-mongo设置,db配置如下(这里只显示一个条目)

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

以下是我试图复制的mongo命令

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

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}) *(输出:[“filename”:“type1.json”,“secs”: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异步问题所以请耐心等待。欢迎提出所有建议:))

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您对.distinct()的使用并不正确。当API实际上采用两个单独的参数(不包括回调)时,您传递的是类似JSON的字符串。因此,真正的代码实际上应该或多或少是您最初展示的内容:

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