MongoDB:是否可以将$ lookup的结果限制为某些字段(作为投影)? [英] MongoDB: Is it possible to limit the results of $lookup to certain fields (as a projection)?

查看:1086
本文介绍了MongoDB:是否可以将$ lookup的结果限制为某些字段(作为投影)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一些代码,伙计们.

Here is some code, guys.

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) {

它工作得很好,这是一个普通的查找.但是,我只需要集合用户"中的几个字段,并且$ lookup返回所有内容.有没有一种方法可以将投影应用于查找结果?我只需要三个字段titlefirstnamelastname.

It works perfectly, it's a plain lookup. However I only need a few fields from the collection "users", and the $lookup returns everything. Is there a way to apply a projection to the lookup results? I only need three fields, title, firstname and lastname.

推荐答案

您可以添加$project阶段以限制$lookup

You can add a $project stage to limit the fields from user array after $lookup

db.collection('bugs').aggregate([{
  $project: {
    "user.title": 1,
    "user.firstname": 1,
    "user.lastname": 1
  }
}]);

这篇关于MongoDB:是否可以将$ lookup的结果限制为某些字段(作为投影)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆