Node.js 聚合后填充嵌套数组字段
我希望在聚合后填充引用模型。 到目前为止,我有这个解决方案。我想填充用户对象。它有姓名、电子邮件、电话字段。聚合工作正常,我得到了预期的结果,但用户字段没有填充。我读过很多类似的解决方案,但不幸的是,没有一个能奏效Node.js 聚合后填充嵌套数组字段,node.js,mongodb,express,mongoose,aggregation-framework,Node.js,Mongodb,Express,Mongoose,Aggregation Framework,我希望在聚合后填充引用模型。 到目前为止,我有这个解决方案。我想填充用户对象。它有姓名、电子邮件、电话字段。聚合工作正常,我得到了预期的结果,但用户字段没有填充。我读过很多类似的解决方案,但不幸的是,没有一个能奏效 exports.getResultToCompetition = asyncHandler(async (req, res, next) => { let competitionId = await Competition.findById(req.params.comp
exports.getResultToCompetition = asyncHandler(async (req, res, next) => {
let competitionId = await Competition.findById(req.params.competitionId).select("_id");
const result = await Result.aggregate([
{
// $match: { competition: mongoose.Types.ObjectId("competitionId") },
$match: { competition: mongoose.Types.ObjectId(competitionId._id) },
},
{
$project: {
points: 1,
kilogramms: 1,
sector: 1,
user: 1,
competition: 1,
place: 1,
},
},
{
$group: {
_id: "$sector",
res: { $push: "$$ROOT" },
},
},
{
$sort: {
kilogramms: -1,
},
},
]);
await Result.populate(result, { path: "user", select: "email name avatar flag" });
res.status(200).json({ success: true, data: result });
});
我也试过了,但没用
await Result.populate(result, {
path: "result",
populate: {
path: "res",
model: "Result",
populate: {
path: "user",
model: "User",
select: "email name avatar flag",
},
},
});
这就是我在《邮递员》中得到的
{
"success": true,
"data": [
{
"_id": "A",
"res": [
{
"_id": "5eaeab6b2b9f246693a65659",
"points": 14,
"kilogramms": 14,
"place": 14,
"sector": "A",
"competition": "5eaeaa5448580765da33165a",
{
"_id": "5eaec018784ea96aa7daeebe",
"points": 23,
"kilogramms": 23,
"place": 12,
"sector": "A",
"competition": "5eaeaa5448580765da33165a",
//Expected output includes the user object
"user": {
"_id": "5eaebf3471aab66a84ae01a4",
"name": "John Mayer",
"email": "johnmayer@gmail.com",
"phone": "123456789"
}
},
},
{
"_id": "5eaec018784ea96aa7daeebe",
"points": 23,
"kilogramms": 23,
"place": 12,
"sector": "A",
"competition": "5eaeaa5448580765da33165a",
"user": "5eaebf3471aab66a84ae01a4"
},
{
"_id": "5eaec028784ea96aa7daeebf",
"points": 12,
"kilogramms": 12,
"place": 14,
"sector": "A",
"competition": "5eaeaa5448580765da33165a",
"user": "5eaebf7f784ea96aa7daeeba"
}
]
},
{
"_id": "B",
"res": [
{
"_id": "5eaec03c784ea96aa7daeec0",
"points": 30,
"kilogramms": 34,
"place": 1,
"sector": "B",
"competition": "5eaeaa5448580765da33165a",
"user": "5eaebfa7784ea96aa7daeebb"
},
{
"_id": "5eaec04c784ea96aa7daeec1",
"points": 21,
"kilogramms": 20,
"place": 2,
"sector": "B",
"competition": "5eaeaa5448580765da33165a",
"user": "5eaebfd4784ea96aa7daeebc"
},
{
"_id": "5eaec05a784ea96aa7daeec2",
"points": 13,
"kilogramms": 13,
"place": 3,
"sector": "B",
"competition": "5eaeaa5448580765da33165a",
"user": "5eaebff5784ea96aa7daeebd"
}
]
}
]
}
首先,您不需要以下行:
let competitionId=wait Competition.findById(请求参数competitionId)。选择(“\u id”);
还请注意,您需要在$group之前对kg
进行排序,否则将无效
最后,您可以使用以下方式获取用户信息:
[
{
"_id": "A",
"res": [
{
"_id": "5eaec018784ea96aa7daeebe",
"competition": "5eaeaa5448580765da33165a",
"kilogramms": 23,
"place": 12,
"points": 23,
"sector": "A",
"user": {
"_id": "5eaebf3471aab66a84ae01a4",
"email": "user2_email",
"name": "User2",
"phone": "user2_phone"
}
},
{
"_id": "5eaeab6b2b9f246693a65659",
"competition": "5eaeaa5448580765da33165a",
"kilogramms": 14,
"place": 14,
"points": 14,
"sector": "A",
"user": {
"_id": "5ead24956ee84737c5910d06",
"email": "user1_email",
"name": "User1",
"phone": "user1_phone"
}
},
{
"_id": "5eaec028784ea96aa7daeebf",
"competition": "5eaeaa5448580765da33165a",
"kilogramms": 12,
"place": 14,
"points": 12,
"sector": "A",
"user": {
"_id": "5eaebf7f784ea96aa7daeeba",
"email": "user3_email",
"name": "User3",
"phone": "user3_phone"
}
}
]
},
{
"_id": "B",
"res": [
{
"_id": "5eaec03c784ea96aa7daeec0",
"competition": "5eaeaa5448580765da33165a",
"kilogramms": 34,
"place": 1,
"points": 30,
"sector": "B",
"user": {
"_id": "5eaebfa7784ea96aa7daeebb",
"email": "user4_email",
"name": "User4",
"phone": "user4_phone"
}
},
{
"_id": "5eaec04c784ea96aa7daeec1",
"competition": "5eaeaa5448580765da33165a",
"kilogramms": 20,
"place": 2,
"points": 21,
"sector": "B",
"user": {
"_id": "5eaebfd4784ea96aa7daeebc",
"email": "user5_email",
"name": "User5",
"phone": "user1_phone"
}
},
{
"_id": "5eaec05a784ea96aa7daeec2",
"competition": "5eaeaa5448580765da33165a",
"kilogramms": 13,
"place": 3,
"points": 13,
"sector": "B",
"user": {
"_id": "5eaebff5784ea96aa7daeebd",
"email": "user6_email",
"name": "User6",
"phone": "user6_phone"
}
}
]
}
]
exports.getResultToCompetition=asyncHandler(异步(req、res、next)=>{
const data=wait Result.aggregate([
{
$match:{competition:mongoose.Types.ObjectId(req.params.competitionId)},
},
{
$项目:{
要点:一,,
千克:1,
部门:1,
用户:1,,
比赛:1,,
地点:1,,
},
},
{
$sort:{
千克:-1,
},
},
{
$lookup:{
from:“users”,//必须是集合的物理名称
localField:“用户”,
foreignField:“\u id”,
作为:“用户”,
},
},
{
$addFields:{
用户:{
$arrayElemAt:[“$user”,0],
},
},
},
{
$group:{
_id:“$扇区”,
res:{
$push:“$$ROOT”,
},
},
},
]);
json({success:true,data});
});
输出如下:
[
{
"_id": "A",
"res": [
{
"_id": "5eaec018784ea96aa7daeebe",
"competition": "5eaeaa5448580765da33165a",
"kilogramms": 23,
"place": 12,
"points": 23,
"sector": "A",
"user": {
"_id": "5eaebf3471aab66a84ae01a4",
"email": "user2_email",
"name": "User2",
"phone": "user2_phone"
}
},
{
"_id": "5eaeab6b2b9f246693a65659",
"competition": "5eaeaa5448580765da33165a",
"kilogramms": 14,
"place": 14,
"points": 14,
"sector": "A",
"user": {
"_id": "5ead24956ee84737c5910d06",
"email": "user1_email",
"name": "User1",
"phone": "user1_phone"
}
},
{
"_id": "5eaec028784ea96aa7daeebf",
"competition": "5eaeaa5448580765da33165a",
"kilogramms": 12,
"place": 14,
"points": 12,
"sector": "A",
"user": {
"_id": "5eaebf7f784ea96aa7daeeba",
"email": "user3_email",
"name": "User3",
"phone": "user3_phone"
}
}
]
},
{
"_id": "B",
"res": [
{
"_id": "5eaec03c784ea96aa7daeec0",
"competition": "5eaeaa5448580765da33165a",
"kilogramms": 34,
"place": 1,
"points": 30,
"sector": "B",
"user": {
"_id": "5eaebfa7784ea96aa7daeebb",
"email": "user4_email",
"name": "User4",
"phone": "user4_phone"
}
},
{
"_id": "5eaec04c784ea96aa7daeec1",
"competition": "5eaeaa5448580765da33165a",
"kilogramms": 20,
"place": 2,
"points": 21,
"sector": "B",
"user": {
"_id": "5eaebfd4784ea96aa7daeebc",
"email": "user5_email",
"name": "User5",
"phone": "user1_phone"
}
},
{
"_id": "5eaec05a784ea96aa7daeec2",
"competition": "5eaeaa5448580765da33165a",
"kilogramms": 13,
"place": 3,
"points": 13,
"sector": "B",
"user": {
"_id": "5eaebff5784ea96aa7daeebd",
"email": "user6_email",
"name": "User6",
"phone": "user6_phone"
}
}
]
}
]
你能给出样本文件和预期输出吗?请补充问题。结果和用户集合文档,以及预期输出。另外,您在postman中获得的内容也不是必需的。我已将预期输出添加到第一个对象中。好的,您可以检查我的答案吗?我还清除了一些不必要的行。是的,谢谢:)我已经删除了第一行。这是不必要的