伙计们,这是一些代码。
db.collection('bugs').aggregate([{
$match: finder
}, {
$sort: { name: 1 }
}, {
$limit: startrecord + settings.pagination_limit
}, {
$skip: startrecord
}, {
$lookup: {
from: 'users',
localField: 'user',
foreignField: '_id',
as: 'user'
}
}], {
collation: collation
}, function(err, docs) {
它工作得很好,这是一个简单的查找。但是我只需要集合“users”中的一些字段,$ lookup返回所有内容。有没有办法将投影应用于查找结果?我只需要三个领域,title
,firstname
和lastname
。
在$project
之后你可以添加一个user
阶段来限制来自$lookup
数组的字段
db.collection('bugs').aggregate([{
$project: {
"user.title": 1,
"user.firstname": 1,
"user.lastname": 1
}
}]);