node.js - 如何在nodejs中按对象ID获取总记录
问题描述
我想获取与类别对象 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,类别对象 ID 存在于作业 json 中。来自 Category 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'
},
]
解决方案
使用mongodb的聚合函数
db.jobs.aggregate([
{ $group: { _id: "$category", total: { $sum: 1 } } }
])
推荐阅读
- c - 拆分字符串并存储到堆算法问题
- ruby-on-rails - 在 Rails 6 上使用 Wepacker 的 Chosen-sprite.png 无法编译:SassError: Invalid UTF-8 sequence
- python - 将最大值时间戳放入 PySpark 中的数组中
- postgresql - 使用 postgresql 作为 .net core 3.0 中的数据库时,代码中的数据迁移首先出错
- go - 在go中将字符串转换为字节
- python - 在 Altair 中保留上一层的工具提示
- c# - 为什么 ConfigurationManager 不包含 OpenExeConfiguration 的定义?C#
- flutter - setState() 未显示在建议的小部件颤动中
- javascript - 来自不同来源的 Cookie
- javascript - Winforms (particles.js) 中的多边形粒子