我如何计算 mongoose 中的管理员数量?
how do i count number of admins in mongoose?
当我执行以下操作时 aggregate.my 预期是按照我下面的输出中的预期获得仪表板的角色计数,但我得到的分别是 0 的管理员、扩展名、播种机值。你认为我的错误是什么?
型号
var Schema = mongoose.Schema(
{
username: String,
email: String,
password: String,
roles: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Role"
}
],
},
{ timestamps: true }
);
这是我正在处理的聚合...
控制器
exports.userRoleCount = (rep,res,next) =>{
User.aggregate([
{$project: {
admin: {$cond: [{$eq: ["$roles", "['admin']"]}, 1, 0]},
seeder: {$cond: [{$eq: ["$roles", "seeder"]}, 1, 0]},
extension: {$cond: [{$eq: ["$roles", "extension"]}, 1, 0]},
}},
{$group: { _id: null, admin: {$sum: "admin"},
seeder: {$sum: "$seeder"},
extension: {$sum: "$extension"},
total: {$sum: 1},
}},
], (error,data)=>{
if (error){
return next(error);
} else {
res.json(data);
}
}
)
这是我得到的输出
输出
{
"_id": null,
"admin": 0,
"seeder": 0,
"extension": 0,
"total": 9
}
我的期望是这样的......
{
"_id": null,
"admin": 3,
"seeder": 4,
"extension": 2,
"total": 9
}
这是我的角色数组...
db.ROLES = ["用户", "管理员", "分机", "播种机"];
Hi,
{$unwind : "$roles"},
{$group: { _id: null,
admin: {$sum: {$eq : ["roles": "admin"]}},
seeder: {$sum: {$eq : ["roles": "seeder"]}},
total: {$sum: 1},
}},
我更改了代码片段如下,它成功了!!
exports.userRoleCount = (rep,res,next) =>{
User.aggregate([
{
"$project": {
"_id": 1,
"roles": 1
}
},
{
"$unwind": "$roles"
},
{
"$group": {
"_id": '$roles',
"count": {"$sum": 1}
}
},
{"$sort": {"_id": -1}},
{"$limit": 5}
], (error,data)=>{
if (error){
return next(error);
} else {
res.json(data);
// console.log(data[1].count);
}
}
)
}
输出如下所示
{
{
_id:1 // this id is for role admin
count: 2
},
{
_id:2 // this id is for role extension
count: 5
}
}
当我执行以下操作时 aggregate.my 预期是按照我下面的输出中的预期获得仪表板的角色计数,但我得到的分别是 0 的管理员、扩展名、播种机值。你认为我的错误是什么?
型号
var Schema = mongoose.Schema(
{
username: String,
email: String,
password: String,
roles: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Role"
}
],
},
{ timestamps: true }
);
这是我正在处理的聚合...
控制器
exports.userRoleCount = (rep,res,next) =>{
User.aggregate([
{$project: {
admin: {$cond: [{$eq: ["$roles", "['admin']"]}, 1, 0]},
seeder: {$cond: [{$eq: ["$roles", "seeder"]}, 1, 0]},
extension: {$cond: [{$eq: ["$roles", "extension"]}, 1, 0]},
}},
{$group: { _id: null, admin: {$sum: "admin"},
seeder: {$sum: "$seeder"},
extension: {$sum: "$extension"},
total: {$sum: 1},
}},
], (error,data)=>{
if (error){
return next(error);
} else {
res.json(data);
}
}
)
这是我得到的输出
输出
{
"_id": null,
"admin": 0,
"seeder": 0,
"extension": 0,
"total": 9
}
我的期望是这样的......
{
"_id": null,
"admin": 3,
"seeder": 4,
"extension": 2,
"total": 9
}
这是我的角色数组... db.ROLES = ["用户", "管理员", "分机", "播种机"];
Hi,
{$unwind : "$roles"},
{$group: { _id: null,
admin: {$sum: {$eq : ["roles": "admin"]}},
seeder: {$sum: {$eq : ["roles": "seeder"]}},
total: {$sum: 1},
}},
我更改了代码片段如下,它成功了!!
exports.userRoleCount = (rep,res,next) =>{
User.aggregate([
{
"$project": {
"_id": 1,
"roles": 1
}
},
{
"$unwind": "$roles"
},
{
"$group": {
"_id": '$roles',
"count": {"$sum": 1}
}
},
{"$sort": {"_id": -1}},
{"$limit": 5}
], (error,data)=>{
if (error){
return next(error);
} else {
res.json(data);
// console.log(data[1].count);
}
}
)
}
输出如下所示
{
{
_id:1 // this id is for role admin
count: 2
},
{
_id:2 // this id is for role extension
count: 5
}
}