Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 聚合方法未按预期工作_Node.js_Mongodb - Fatal编程技术网

Node.js 聚合方法未按预期工作

Node.js 聚合方法未按预期工作,node.js,mongodb,Node.js,Mongodb,以下是我的收藏,名为Run var Run = [{ ID: 1, Name: 'TestRun', Comments: 'TestRun', Status: [{ Cur: 'Active', Mapper: { Area: 'BH', State: 'MH' } },

以下是我的收藏,名为Run

var Run = [{
        ID: 1,
        Name: 'TestRun',
        Comments: 'TestRun',
        Status: [{
            Cur: 'Active',
            Mapper: {
                Area: 'BH',
                State: 'MH'
            }
        },
        {
            Cur: 'Inactive',
            Mapper: {
                Area: 'BO',
                State: 'DU'
            }
        }]
    }]
当我用
'Area'
作为
'BH'
获取所有数据时,我想创建一个查询,以便得到如下结果

    Status: [{
        Cur: 'Active',
        Mapper: {
             Area: 'BH',
             State: 'MH'
        }
   }]
当我在mongodb提示符中触发此查询时:

db.Run.aggregate([ 
    {$match: {'Status.Mapper.Area': 'BO'}},
        {$project: { 
            Status: 
                {$filter: { 
                    input: '$Status', 
                    as: 'script', 
                    cond: {$eq: ['$$script.Mapper.Area','BO']}
            }},
        _id: 0
    }} 
]).pretty();
结果如预期。但是,当我尝试使用Nodejs运行此查询时,它不会返回任何结果

var mongodb = require('mongodb');
var uri = 'mongodb://localhost:27017/exeexample';

mongodb.MongoClient.connect(uri, function(error, db) {
    if(error) {
        console.log(error);
        process.exit(1);
    }

    db.collection('Run').find({ID: 1}).toArray(function(error, docs) {
            if(error) {
                console.log(error);
                process.exit(1);
            }

            console.log('found docs:1 ');
            docs.forEach(function(doc) {
                console.log(JSON.stringify(doc));
                db.collection('Run').aggregate([
                    {$match: {'Status.Mapper.Area': 'BO'}},
                    {$project: {
                        Status: {$filter: {
                            input: '$Status',
                            as: 'script',
                            cond: {$eq: ['$$script.Mapper.Area', 'BO']}
                        }},
                        _id: 0
                    }}
                ]).toArray(function(error, docs) {
                    console.log('found docs:2 ');
                docs.forEach(function(doc) {
                    console.log('found docs:3 ');
                    console.log(JSON.stringify(doc));
                }); 
            }); 
            process.exit(0);
        });
    });
});
该查询与我在MongoDB提示符中使用的查询几乎相同。因为没有抛出错误,所以filter子句似乎有问题。 感谢你在这方面的帮助

进程退出(0)放错位置,导致在异步聚合调用完成之前执行此语句。幸亏 更新代码如下:

var mongodb = require('mongodb');
var uri = 'mongodb://localhost:27017/exeexample';

mongodb.MongoClient.connect(uri, function(error, db) {
    if(error) {
        console.log(error);
        process.exit(1);
    }

    db.collection('Run').find({ID: 1}).toArray(function(error, docs) {
            if(error) {
                console.log(error);
                process.exit(1);
            }

            console.log('found docs:1 ');
            docs.forEach(function(doc) {
                console.log(JSON.stringify(doc));
                db.collection('Run').aggregate([
                    {$match: {'Status.Mapper.Area': 'BO'}},
                    {$project: {
                        Status: {$filter: {
                            input: '$Status',
                            as: 'script',
                            cond: {$eq: ['$$script.Mapper.Area', 'BO']}
                        }},
                        _id: 0
                    }}
                ]).toArray(function(error, docs) {
                    console.log('found docs:2 ');
                docs.forEach(function(doc) {
                    console.log('found docs:3 ');
                    console.log(JSON.stringify(doc));
                }); 
             process.exit(0); **//Moved**
            });   
        });
    });
});
process.exit(0)放错位置,导致在异步聚合调用完成之前执行此语句。幸亏
更新代码如下:

var mongodb = require('mongodb');
var uri = 'mongodb://localhost:27017/exeexample';

mongodb.MongoClient.connect(uri, function(error, db) {
    if(error) {
        console.log(error);
        process.exit(1);
    }

    db.collection('Run').find({ID: 1}).toArray(function(error, docs) {
            if(error) {
                console.log(error);
                process.exit(1);
            }

            console.log('found docs:1 ');
            docs.forEach(function(doc) {
                console.log(JSON.stringify(doc));
                db.collection('Run').aggregate([
                    {$match: {'Status.Mapper.Area': 'BO'}},
                    {$project: {
                        Status: {$filter: {
                            input: '$Status',
                            as: 'script',
                            cond: {$eq: ['$$script.Mapper.Area', 'BO']}
                        }},
                        _id: 0
                    }}
                ]).toArray(function(error, docs) {
                    console.log('found docs:2 ');
                docs.forEach(function(doc) {
                    console.log('found docs:3 ');
                    console.log(JSON.stringify(doc));
                }); 
             process.exit(0); **//Moved**
            });   
        });
    });
});

我一试就行了。请确保在这两种情况下使用相同的数据库。抱歉,我以为只有整个代码的一小部分可以帮助解决我的查询。我已经用整个脚本更新了我的问题。请检查您在shell中尝试此操作时是否也在使用
exexecample
数据库(它正在工作)?是的,我在shell中使用execample练习。您正在调用
process.exit(0)aggregate
调用完成之前执行code>操作。我尝试使用它时效果很好。请确保在这两种情况下使用相同的数据库。抱歉,我以为只有整个代码的一小部分可以帮助解决我的查询。我已经用整个脚本更新了我的问题。请检查您在shell中尝试此操作时是否也在使用
exexecample
数据库(它正在工作)?是的,我在shell中使用execample练习。您正在调用
process.exit(0)在异步
聚合
调用完成之前。