Arrays $lookup中的$limit文档数

Arrays $lookup中的$limit文档数,arrays,json,mongodb,aggregation-framework,aggregate,Arrays,Json,Mongodb,Aggregation Framework,Aggregate,我在返回此结果的查询中遇到问题: { "_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"), "search" : "flarize", "name" : "flarize", "color" : 0, "profil" : "", "banner" : "", "desc" : "", "date" : 1540501286109, "friend" : [ [

我在返回此结果的查询中遇到问题:

{
    "_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
    "search" : "flarize",
    "name" : "flarize",
    "color" : 0,
    "profil" : "",
    "banner" : "",
    "desc" : "",
    "date" : 1540501286109,
    "friend" : [
            [        
                    {
                            "_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
                            "search" : "flarize",
                            "name" : "flarize",
                            "email" : "flarize.b73@gmail.com",
                            "password" : "$2a$10$eYeOtEkEUyD7TFkjKvhZOuSSpvBolkL17TrPHuoHhOT8JrsQR0UKW",
                            "color" : 0,
                            "profil" : "",
                            "banner" : "",
                            "desc" : "",
                            "date" : 1540501286109,
                            "friend" : [
                                    {
                                            "id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
                                            "date" : 1540572026419
                                    },
                                    {
                                            "id" : ObjectId("5bd19a92da24674fdabd26b6"),
                                            "date" : 1540572026419
                                    }
                            ],
                            "groupes" : [ ]
                    }
            ]
    ]
}
但这并不是我真正想要的,我希望这些字段被隐藏:friend.password、friend.email和friend.groups和friend.friend的结果限制为10。我不知道怎样才能解决这个问题。我的请求:

db.users.aggregate(
    {$match:
        {search:"flarize"}
    },
    {$lookup:
        {from:"users", 
         localField:"friend.id", 
         foreignField:"_id", 
         as:"friend"
    }},
    {$project:
        { search: 1, 
         name: 1, 
         profil: 1, 
         banner: 1, 
         color: 1, 
         date: 1, 
         desc: 1, 
         friend: [{$slice:["$friend", 0, 10]}]
    }
}).pretty();
一份用户文件:

{
    "_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
    "search" : "flarize",
    "name" : "flarize",
    "email" : "theo.ba73@gmail.com",
    "password" : "$2a$10$eYeOtEkEUyD7TFkjKvhZOuSSpvBolkL17TrPHuoHhOT8JrsQR0UKW",
    "color" : 0,
    "profil" : "",
    "banner" : "",
    "desc" : "",
    "date" : 1540501286109,
    "friend" : [
            {
                    "id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
                    "date" : 1540572026419
            },
            {
                    "id" : ObjectId("5bd19a92da24674fdabd26b6"),
                    "date" : 1540572026419
            }
    ],
    "groupes" : [ ]
 }

谢谢你的帮助。

你可以在下面试试

db.users.aggregate([
  { $match: { search: "flarize" } },
  { $lookup: {
    from: "users", 
    let: { friendId: "$friend.id" }, 
    pipeline: [
      { $match: { $expr: { $in: ["$_id", "$$friendId"] }}},
      { $limit: 10 },
      { $project: { email: 0, password: 0 }}
    ], 
    as: "friend"
  }}
])

你能展示一下你的示例文档和你正在使用的mongo版本吗?为什么你要与用户一起查找你也在与用户聚合。4.0.3对于版本和Kuldeep Mishra,我不知道是否有其他方法更新了answerNow friend字段为空