Mongodb $lookup聚合无法显示数组值

Mongodb $lookup聚合无法显示数组值,mongodb,aggregate,lookup,Mongodb,Aggregate,Lookup,我和MongoDB合作,我有两个系列 数据收集[“用户配置文件”] { "_id" : ObjectId("60650e6fc4b4603e1e78bb23"), "firstName" : "Luthfan", "lastName" : "Difiesa", "mobile" : "86742633497&

我和MongoDB合作,我有两个系列

数据收集[“用户配置文件”]

{ 
    "_id" : ObjectId("60650e6fc4b4603e1e78bb23"), 
    "firstName" : "Luthfan", 
    "lastName" : "Difiesa", 
    "mobile" : "86742633497", 
    "gender" : "male",
    "createdOn" : ISODate("2021-04-22T05:26:07.428+0000"), 
    "updatedOn" : ISODate("2021-04-22T05:26:55.218+0000")
}
{ 
    "_id" : ObjectId("60650e7a1a4a817a1dd0a29c"), 
    "userId" : "86742633497", 
    "contents" : [
        {
            "_id" : ObjectId("5ef9d2da228f840bbd41649c"), 
            "name" : "Kelas 11"
        }
    ]
}
数据收集[“用户愿望列表”]

{ 
    "_id" : ObjectId("60650e6fc4b4603e1e78bb23"), 
    "firstName" : "Luthfan", 
    "lastName" : "Difiesa", 
    "mobile" : "86742633497", 
    "gender" : "male",
    "createdOn" : ISODate("2021-04-22T05:26:07.428+0000"), 
    "updatedOn" : ISODate("2021-04-22T05:26:55.218+0000")
}
{ 
    "_id" : ObjectId("60650e7a1a4a817a1dd0a29c"), 
    "userId" : "86742633497", 
    "contents" : [
        {
            "_id" : ObjectId("5ef9d2da228f840bbd41649c"), 
            "name" : "Kelas 11"
        }
    ]
}
预期输出:

{ 
    "_id" : ObjectId("60650e6fc4b4603e1e78bb23"), 
    "firstName" : "Luthfan", 
    "lastName" : "Difiesa", 
    "mobile" : "86742633497", 
    "gender" : "male",
    "contents" : [
        {
            "_id" : ObjectId("5ef9d2da228f840bbd41649c"), 
            "name" : "Kelas 11"
        }
    ]
}
db.getCollection("user-profile").aggregate(
    [ 
        { 
            "$lookup" : { 
                "from" : "user-wishlist", 
                "localField" : "mobile", 
                "foreignField" : "userId", 
                "as" : "contents"
            }
        }
    ], 
    { 
        "allowDiskUse" : false
    }
);
{ 
    "_id" : ObjectId("60650e6fc4b4603e1e78bb23"), 
    "firstName" : "Luthfan", 
    "lastName" : "Difiesa", 
    "mobile" : "86742633497", 
    "gender" : "male",
    "contents" : [

    ]
}
以下是查询:

{ 
    "_id" : ObjectId("60650e6fc4b4603e1e78bb23"), 
    "firstName" : "Luthfan", 
    "lastName" : "Difiesa", 
    "mobile" : "86742633497", 
    "gender" : "male",
    "contents" : [
        {
            "_id" : ObjectId("5ef9d2da228f840bbd41649c"), 
            "name" : "Kelas 11"
        }
    ]
}
db.getCollection("user-profile").aggregate(
    [ 
        { 
            "$lookup" : { 
                "from" : "user-wishlist", 
                "localField" : "mobile", 
                "foreignField" : "userId", 
                "as" : "contents"
            }
        }
    ], 
    { 
        "allowDiskUse" : false
    }
);
{ 
    "_id" : ObjectId("60650e6fc4b4603e1e78bb23"), 
    "firstName" : "Luthfan", 
    "lastName" : "Difiesa", 
    "mobile" : "86742633497", 
    "gender" : "male",
    "contents" : [

    ]
}
但结果如下:

{ 
    "_id" : ObjectId("60650e6fc4b4603e1e78bb23"), 
    "firstName" : "Luthfan", 
    "lastName" : "Difiesa", 
    "mobile" : "86742633497", 
    "gender" : "male",
    "contents" : [
        {
            "_id" : ObjectId("5ef9d2da228f840bbd41649c"), 
            "name" : "Kelas 11"
        }
    ]
}
db.getCollection("user-profile").aggregate(
    [ 
        { 
            "$lookup" : { 
                "from" : "user-wishlist", 
                "localField" : "mobile", 
                "foreignField" : "userId", 
                "as" : "contents"
            }
        }
    ], 
    { 
        "allowDiskUse" : false
    }
);
{ 
    "_id" : ObjectId("60650e6fc4b4603e1e78bb23"), 
    "firstName" : "Luthfan", 
    "lastName" : "Difiesa", 
    "mobile" : "86742633497", 
    "gender" : "male",
    "contents" : [

    ]
}

不是因为集合名称使用了外来或本地字段中的特殊字符或类型数据吗?谢谢大家。

您的查询看起来不错,只需在查找后添加一个阶段即可获得您想要的结果

{
  $addFields: {
    contents: {
      $arrayElemAt: ["$contents.contents", 0]
    }
  }
}

查询看起来不错,应该可以运行,请验证数据库中的集合名称
用户愿望列表
。该查询在操场上运行:我更新了集合名称并添加了2列(createdOn和updateOn)。当我在playgroud[link]()中尝试它时,我得到了一个错误,无法解析date@turivishalcheck localField和foreignField的数据类型都是相同的,它们都是string@beingnin我想你的代码中会有一个缺陷,认为
$arrayElemAt
只返回
contents
数组中的第一项,即使它有多个项。令人惊讶的是,事实并非如此。它将返回内容数组中的所有项。我不明白将结果与$addFields阶段和没有$addFields阶段进行比较,
用户愿望列表
集合有键
内容
,根据问题的预期结果,他只想在结果中显示该字段。我确实理解您为什么添加了
$addFields
阶段。但让我困惑的是,我希望
arrayElemAt
操作符只返回
$contents.contents
数组中的第一项,因为您已经清楚地提到了0作为索引。但当我在操场上用内容数组中的多个项目尝试它时,它仍然返回所有项目。
contents.contents
是它的嵌套数组,因此它将返回2级嵌套数组
[[]]
,因此我们从第一个元素中选择了元素。只需查看您的澄清,在查找阶段之后添加新阶段
{$addFields:{contents:$contents.contents}
。我想我现在明白了。它不是像我想的那样从
$contents.contents
数组中获取第一项。相反,它从
contents
数组中获取第一项的
contents
字段(这是一个数组)