mongodb 如何在nodejs中按对象id获取总记录

cygmwpex  于 2023-01-12  发布在  Go
关注(0)|答案(2)|浏览(138)

我想得到所有类别名称和总记录相对于类别对象的id。
我是新的JS节点,想创建一个API的清单和总记录与各自的对象ID。
我对类别的看法是-

/* 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和类别对象id出现在jobs json中。就像类别JSON中的Test类别在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'
},
]
zynd9foi

zynd9foi1#

使用mongodb的聚合函数

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

pjngdqdw2#

使用填充

const x= await X.find()
        .populate('userWhoCreated', 'field1 field2')
        .populate('category', 'field1 field2');
      res.json({
        x,
      });

相关问题