如何在Node.js中按对象ID获取总记录

时间:2019-08-23 10:59:42

标签: node.js mongodb

我想获取有关类别对象ID的所有类别名称和总记录。

我是Node JS的新手,并希望创建用于列出和记录具有相应对象ID的api。

我的类别的json是-

/* 1 createdAt:8/21/2019, 3:00:35 PM*/
{
    "_id" : ObjectId("5d5d0f3b3dff690eac4872ee"),
    "isForHomePage" : false,
    "isactive" : false,
    "name" : "Welcome",
    "description" : "Solutions for Your Business",
    "catImage" : "http://localhost:3000/static/media/software-service.1ec2246b.jpg",
    "createdTime" : 1566379835,
    "__v" : 0
},

/* 2 createdAt:8/19/2019, 12:17:45 PM*/
{
    "_id" : ObjectId("5d5a4611eb5bc029d4463406"),
    "isForHomePage" : true,
    "isactive" : false,
    "name" : "Test",
    "description" : "Solutions for Your Business",
    "catImage" : "http://localhost:3000/static/media/software-service.1ec2246b.jpg",
    "createdTime" : 1566197265,
    "__v" : 0
},

/* 3 createdAt:8/19/2019, 12:10:01 PM*/
{
    "_id" : ObjectId("5d5a44417d10952b50ff13a0"),
    "isForHomePage" : true,
    "isactive" : true,
    "name" : "Software Services",
    "description" : "Solutions for Your Business",
    "catImage" : "http://localhost:3000/static/media/software-service.1ec2246b.jpg",
    "createdTime" : 1566196801,
    "__v" : 0
},

/* 4 createdAt:8/19/2019, 12:07:51 PM*/
{
    "_id" : ObjectId("5d5a43bf7d10952b50ff139f"),
    "isForHomePage" : true,
    "isactive" : true,
    "name" : "Analytics",
    "description" : "Solutions for Your Business",
    "catImage" : "http://localhost:3000/static/media/analytics.cf89d7fe.jpg",
    "createdTime" : 1566196671,
    "__v" : 0
}

作业的JSON是-

/* 1 createdAt:8/22/2019, 12:48:08 PM*/
{
    "_id" : ObjectId("5d5e41b0807a2504e15dcc01"),
    "status" : 8001,
    "duration" : 10,
    "isactive" : true,
    "userWhoCreated" : ObjectId("5d5d40276ab29a4daef653ae"),
    "companyNane" : "Sanganan It Solutions Pvt. Ltd.",
    "contactPerson" : "Gaurav Sinha",
    "jobTitle" : "iOS Developer",
    "category" : ObjectId("5d5a4611eb5bc029d4463406"),
    "description" : "iOS Developer lead requirement",
    "descriptionLink" : "www.abc.com",
    "createdTime" : 1566458288,
    "__v" : 0
},

/* 2 createdAt:8/22/2019, 12:17:31 PM*/
{
    "_id" : ObjectId("5d5e3a83979672041fee4d0a"),
    "status" : 8002,
    "duration" : 10,
    "isactive" : true,
    "userWhoCreated" : ObjectId("5d5d40276ab29a4daef653ae"),
    "companyNane" : "Sanganan It Solutions Pvt. Ltd.",
    "contactPerson" : "Gaurav Sinha",
    "jobTitle" : "iOS Developer",
    "category" : ObjectId("5d5a4611eb5bc029d4463406"),
    "description" : "iOS Developer lead requirement",
    "descriptionLink" : "www.abc.com",
    "createdTime" : 1566456451,
    "__v" : 0
}

我们有类别json,并且作业json中存在类别对象ID。像来自类别JSON的测试类别一样,JOB JSON中有两个作业。

我已经尝试过此解决方案-

router.get("/getAllCategory", function (req, res) {

    categoryObj.find({ 'isactive': true }, function (err, CategoryList) {
        if (err) {
            var message = { "Success": 0, "Message": "Some error found" };
            res.send(message)
        }
        else {
            var message = { "Success": 1, "Category": CategoryList };
            res.send(message)
        }
    })
});

但是它给了我不计数的类别列表。

我期望的输出json必须是这样-

[
{
'categoryname': 'Analytics',
'totalJobsOfThisCategory':'0'
},
{
'categoryname': 'Software Services',
'totalJobsOfThisCategory':'0'
},
{
'categoryname': 'Test',
'totalJobsOfThisCategory':'2'
},
]

1 个答案:

答案 0 :(得分:0)

使用mongodb的聚合函数

db.jobs.aggregate([
   { $group: { _id: "$category", total: { $sum: 1 } } }
])