如何编写 Mongodb 查询来计算集合行数
How to write a Mongodb query to count collection rows
我正在努力实现以下目标。我有一个基于类别的待办事项集合。它有 x 行数。内置了将这些待办事项分配给所有者的功能。我想显示每个类别的作业进度。我怎样才能让这个查询正确。感谢帮助
类别 = [{ id: DB, Title: Database, Desc: Database Fixes, }, { id: NET, Title: Network, Desc: Network Fixes, }, ...]
县=
[{
id: AL
category_id: DB,
Title: Alabama,
},
{
id: AK
category_id: DB,
Title: Alaska
},
{
id: AZ
category_id: DB,
Title: Arizona
},
...]
待办事项=
id: TD1
County_id: AL,
Title: Installation,
assigned: 1,
user: ASED
},
{
id: TD2
County_id: AL,
Title: Documentation,
assigned: 0,
user: ""
},
{
id: TD3
County_id: AL,
Title: Migration,
assigned: 1,
user: ILKQ
},
{
id: TD4
County_id: AK,
Title: Documentation,
assigned: 0,
user: ""
},
{
id: TD5
County_id: AZ,
Title: Testing,
assigned: 0,
user: ""
},
{
id: TD6
County_id: AZ,
Title: Documentation,
assigned: 1,
user: JUSJ
}]
预期结果=
[ {
category_id: DB,
Title: Database,
Desc: Database Fixes,
total_rec: 4,
total_assigned: 2
},
{
category_id: NET,
Title: Network,
Desc: Network Fixes,
total_rec: 2,
total_assigned:1
}]
我猜你想要这样的东西:
db.Category.aggregate([
{
"$lookup": {
"from": "county",
"localField": "id",
"foreignField": "category_id",
"as": "county"
}
},
{
$project: {
Desc: 1,
county: "$county.id",
Title: 1,
id: 1
}
},
{
"$lookup": {
"from": "ToDo",
"localField": "county",
"foreignField": "County_id",
"as": "tasks"
}
},
{
"$project": {
"total_assigned": {
"$sum": {
"$sum": "$tasks.assigned"
}
},
"Desc": 1,
total_rec: {
$size: "$tasks"
},
category_id: "$id",
Title: 1,
_id: 0
}
},
])
从类别开始,使用 $lookup
获取所有国家,然后使用另一个获取所有 ToDos。你可以在操场上看到它是如何工作的here。
顺便说一句,请注意我需要在您的示例中更改亚利桑那州的 category_id
以匹配您想要的结果...
我正在努力实现以下目标。我有一个基于类别的待办事项集合。它有 x 行数。内置了将这些待办事项分配给所有者的功能。我想显示每个类别的作业进度。我怎样才能让这个查询正确。感谢帮助
类别 = [{ id: DB, Title: Database, Desc: Database Fixes, }, { id: NET, Title: Network, Desc: Network Fixes, }, ...]
县=
[{
id: AL
category_id: DB,
Title: Alabama,
},
{
id: AK
category_id: DB,
Title: Alaska
},
{
id: AZ
category_id: DB,
Title: Arizona
},
...]
待办事项=
id: TD1
County_id: AL,
Title: Installation,
assigned: 1,
user: ASED
},
{
id: TD2
County_id: AL,
Title: Documentation,
assigned: 0,
user: ""
},
{
id: TD3
County_id: AL,
Title: Migration,
assigned: 1,
user: ILKQ
},
{
id: TD4
County_id: AK,
Title: Documentation,
assigned: 0,
user: ""
},
{
id: TD5
County_id: AZ,
Title: Testing,
assigned: 0,
user: ""
},
{
id: TD6
County_id: AZ,
Title: Documentation,
assigned: 1,
user: JUSJ
}]
预期结果=
[ {
category_id: DB,
Title: Database,
Desc: Database Fixes,
total_rec: 4,
total_assigned: 2
},
{
category_id: NET,
Title: Network,
Desc: Network Fixes,
total_rec: 2,
total_assigned:1
}]
我猜你想要这样的东西:
db.Category.aggregate([
{
"$lookup": {
"from": "county",
"localField": "id",
"foreignField": "category_id",
"as": "county"
}
},
{
$project: {
Desc: 1,
county: "$county.id",
Title: 1,
id: 1
}
},
{
"$lookup": {
"from": "ToDo",
"localField": "county",
"foreignField": "County_id",
"as": "tasks"
}
},
{
"$project": {
"total_assigned": {
"$sum": {
"$sum": "$tasks.assigned"
}
},
"Desc": 1,
total_rec: {
$size: "$tasks"
},
category_id: "$id",
Title: 1,
_id: 0
}
},
])
从类别开始,使用 $lookup
获取所有国家,然后使用另一个获取所有 ToDos。你可以在操场上看到它是如何工作的here。
顺便说一句,请注意我需要在您的示例中更改亚利桑那州的 category_id
以匹配您想要的结果...