我有以下查询:
db.roles.aggregate([
{'$unwind': '$skills'},
{'$lookup': {
'from': 'skills',
'localField': 'skills._id',
'foreignField': '_id',
'as': 'skillsInfo'
}},
{'$unwind': '$skillsInfo'},
{'$addFields': {'skills': {'$mergeObjects': ['$skills', '$skillsInfo']}}},
{'$project': {'skillsInfo': 0}},
{'$group': {
'_id': '$_id',
'name': {'$first': '$name'},
'description': {'$first': '$description'},
'departments': {'$first': '$departments'},
'compensation': {'$first': '$compensation'},
'skills': {'$push': '$skills'},
'deep' : {'$first' : '$deep'},
'range': {'$first' : '$range'},
'employees': {'$first' : '$employees'}
}},
{'$project': {
'code': 1,
'deep': 1,
'description': 1,
'employees': 1,
'compensation': 1,
'name': 1,
'range': 1,
'skills': {
'$map': {
'input': "$skills",
'as': 'skill',
'in': {
"code" : "$$skill._id",
"name" : "$$skill.name",
"weight" : "$$skill.weight",
"type" : "$$skill.type"
}
}
}
}
}
])
这样,我必须在项目中指定{'field':1}才能检索该字段。有没有一种方法可以只在我想要的字段上使用map来使查询“更清洁”,并同时检索所有字段而不指定它们?