按角色猫鼬搜索



我试图检索基于角色的用户,但每个角色查询,我也想检索管理员了。我不知道该怎么做。这是我目前基于角色

进行检索的方式
public async listAllStaffs(query: ListStaffsRequestQueryDTO) {
var conditions = { }
if (query.role) {
conditions = { role: query.role, 'hub.id': query.merchant };
} else {
conditions = {'hub.id': query.merchant };
}

const data = { page: query.page, limit: query.limit, conditions: conditions};
const all = await this.list(data);
const pagination = {
page: all.page,
limit: all.limit,
rowCount: all.rowCount,
pageCount: all.pageCount
};
const staffs = all.staffs;
return { staffs, pagination };
}

async list(query: PaginationQuery): Promise<any> {
const page = Number(query.page) - 1 || 0;
let limit = Number(query.limit) || 20;
const offset = page * limit;
const sort = query.sort || 'createdAt';
const archived = this.convertArchived(query.archived);
const conditions = {
...query.conditions,
...(!archived
? { deletedAt: undefined }
: { deletedAt: { $ne: undefined } })
};
const staffs = await this.model
.find(conditions)
.select(query.projections)
.skip(offset)
.limit(limit)
.sort(sort);
const totalDocuments = await this.model.countDocuments(conditions);
const result = {
staffs,
page: Number(page) + 1,
limit: Number(limit),
rowCount: Number(totalDocuments),
pageCount: Math.ceil(Number(totalDocuments) / limit)
};
return result;
}

例如,如果角色是operation,我还想返回用户的admin

这样操作人员和管理人员都返回了。

使用$in运算符,查找多个角色中的users,如下所示:

if (query.role) {
conditions = { role: { "$in": [query.role, "admin"] }, 'hub.id': query.merchant 
};
}

最新更新