Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.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 如何使用聚合MongoDB 3.0版获取Mongo数据_Node.js_Mongodb_Mongoose_Aggregation Framework - Fatal编程技术网

Node.js 如何使用聚合MongoDB 3.0版获取Mongo数据

Node.js 如何使用聚合MongoDB 3.0版获取Mongo数据,node.js,mongodb,mongoose,aggregation-framework,Node.js,Mongodb,Mongoose,Aggregation Framework,假设我们有10个集合,那么我们必须根据tag_id找到计数。例如,如果tag_id包含0和1,那么我们必须计算所有数据,以及计算没有tag_id的数据,或者tag_id为null的数据。然后,如果它有未读:false,则输出来,计算所有未读的 查找tag_id的计数和false时未读的计数 { "_id": ObjectId("5912c7240520df77f0c2c18a"), "email_id": "54", "unread": "fals

假设我们有10个集合,那么我们必须根据tag_id找到计数。例如,如果tag_id包含0和1,那么我们必须计算所有数据,以及计算没有tag_id的数据,或者tag_id为null的数据。然后,如果它有未读:false,则输出来,计算所有未读的

查找tag_id的计数和false时未读的计数

{
        "_id": ObjectId("5912c7240520df77f0c2c18a"),
        "email_id": "54",
        "unread": "false",
        "__v": NumberLong(0),
        "tag_id": ["0"

        ]
    }, {
        "_id": ObjectId("5912c71e0520df77f0c2c189"),
        "email_id": "55",
        "unread": "false",
        "__v": NumberLong(0),
        "tag_id": [
            "1"
        ]
    }, {
        "_id": ObjectId("5912c71d0520df77f0c2c186"),
        "email_id": "51",
        "unread": "false",
        "__v": NumberLong(0),
        "tag_id": [
            "2", "1"
        ]
    }
预期结果:

{
    "data": [{
        "tag_id": "1",
        "count_email": 1,(count of email on the basis of tag_id)
        "unread": 9(count the unread on the basis of output of tag_id)
    }, {
        "tag_id": "3",
        "count_email": 45,
        "unread": 3
    }, {
        "tag_id": "2",
        "count_email": 5,
        "unread": 4
    }, {
        "id": null,
        "count_email": 52,
        "unread": 35
    }]
}
请尝试以下代码 参考-


它给了我意外的错误标记,预期为,(70:24)68 | 69 | req.email.aggregate([>70 |$project:您能修复此错误以便我可以使用它吗?我以为您是从DB获取这些集合。req.email是什么?您能粘贴整个错误消息吗?这是一个语法错误SyntaxError:/var/www/html/hr_system/src/controllers/fetch_email.js:意外标记,预期,(70:24)68 | 69 | req.email.aggregate([>70 |$project:[^71 |{72 | tag|u id:'$tag|u id',73 | unredcount:{$cond:[{$eq:['$unread','true']},1,0]}DB.aggregate([{$project:{tag_id:'$tag_id',unreadcount:{$cond:[{$eq:['$unread','true']},1,0]}}}{$group:{{tag id:'$tag_id',unread:{$sum:'$unreadcount'},}],函数(错误,结果){console.log(结果);})在项目之前观察花支架,我错过了它。
DB.aggregate([
                {$project:
                {
                    tag_id: '$tag_id',
                    unreadcount: { $cond: [ { $eq: [ '$unread', 'true' ] }, 1, 0 ] }
                }},
                { $group: {
                    _id: '$tag_id',
                    unread: { $sum: '$unreadcount'},
                }}
            ], function (err, results) {
                console.log(results);
            })