繁体   English   中英

Mongodb聚合查找仅返回一个数组字段

[英]Mongodb aggregate lookup return only one field of array

我有一些项目的收藏品。

  • Casts集合包含电影演员表
  • 内容集合包含电影内容

我想运行聚合查找以获取有关位置类型的电影转换的信息。

我删除了不必要字段的集合细节

投射细节:

{
    "_id" : ObjectId("5a6cf47415621604942386cd"),
    "fa_name" : "",
    "en_name" : "Ehsan",
    "fa_bio" : "",
    "en_bio" : ""
}

内容详情:

{
    "_id" : ObjectId("5a6b8b734f1408137f79e2cc"),

    "casts" : [ 
        {
            "_id" : ObjectId("5a6cf47415621604942386cd"),
            "fa_fictionName" : "",
            "en_fictionName" : "Ehsan2",
            "positionType" : {
                "id" : 3,
                "fa_name" : "",
                "en_name" : "Director"
            }
        }, 
        {
            "_id" : ObjectId("5a6cf47415621604942386cd"),
            "fa_fictionName" : "",
            "en_fictionName" : "Ehsan1",
            "positionType" : {
                "id" : 3,
                "fa_name" : "",
                "en_name" : "Writers"
            }
        }
    ],
    "status" : 0,
    "created" : Timestamp(1516997542, 4),
    "updated" : Timestamp(1516997542, 5)
}

当我用bellow查询运行聚合查找时,在新生成的查找数组中只有一个转换内容如果按照上面的转换数组值聚合查找应该返回两个类型的转换内容。 在强制转换数组值中存在两种类型的强制转换,1)作者和导演。 但是返回的导演投下了内容。 _cast应该包含两个对象而不是一个对象!

聚合查询查询:

{$lookup:{from:"casts",localField:"casts._id",foreignField:"_id",as:"_casts"}}

结果:

{
        "_id" : ObjectId("5a6b8b734f1408137f79e2cc"),

        "casts" : [ 
            {
                "_id" : ObjectId("5a6cf47415621604942386cd"),
                "fa_fictionName" : "",
                "en_fictionName" : "Ehsan2",
                "positionType" : {
                    "id" : 3,
                    "fa_name" : "",
                    "en_name" : "Director"
                }
            }, 
            {
                "_id" : ObjectId("5a6cf47415621604942386cd"),
                "fa_fictionName" : "",
                "en_fictionName" : "Ehsan1",
                "positionType" : {
                    "id" : 3,
                    "fa_name" : "",
                    "en_name" : "Writers"
                }
            }
        ],
    "_casts" : [ 
           {
            "_id" : ObjectId("5a6cf47415621604942386cd"),
            "fa_name" : "",
            "en_name" : "Ehsan",
            "fa_bio" : "",
            "en_bio" : ""
           }
        ],
        "status" : 0,
        "created" : Timestamp(1516997542, 4),
        "updated" : Timestamp(1516997542, 5)
    }

编辑-1最后我的问题解决了。 我对此查询只有一个问题,此查询不显示根文档字段。 终于解决了这个问题。 最终查询存在于EDIT-2中。

查询:

db.contents.aggregate([ 
{"$unwind":"$casts"},
{"$lookup":{"from":"casts","localField":"casts._id","foreignField":"_id","as":"casts.info"}},
{"$unwind":"$casts.info"},
{"$group":{"_id":"$_id", "casts":{"$push":"$casts"}}},
])

编辑-2

db.contents.aggregate([ 
{"$unwind":"$casts"},
{"$lookup":{"from":"casts","localField":"casts._id","foreignField":"_id","as":"casts.info"}},
{"$unwind":"$casts.info"},
{$group:{"_id":"$_id", "data":{"$first":"$$ROOT"}, "casts":{"$push":"$casts"}}},
{$replaceRoot:{"newRoot":{"$mergeObjects":["$data",{"casts‌​":"$casts"}]}}},
{$project:{"casts":0}}
]).pretty()

这是预期的行为。

docs

如果您的localField是一个数组,您可能需要向管道添加$ unwind阶段。 否则,localField和foreignField之间的相等条件是foreignField:{$ in:[localField.elem1,localField.elem2,...]}。

因此,要使用外部字段元素连接每个本地字段数组元素,您必须$unwind本地数组。

db.content.aggregate([
  {"$unwind":"$casts"},
  {"$lookup":{"from":"casts","localField":"casts._id","foreignField":"_id","as":"_casts"}}
])

您的Casts集合仅显示1个文档。 同样,您的内容集合仅显示1个文档。

这是1比1 - 而不是1比2.聚合按设计工作。

内容文档有2个“强制转换”。 这两个演员阵营是子文件。 将这些作为子文档使用,或重新设计您的集合。 我不喜欢使用子文档,除非我知道我不需要将它们用作查找或加入它们。

我建议你重新设计你的收藏品。

你的内容集(它让我想起“电影”)看起来像这样:

_id
title
releaseDate
genre
etc.

您可以像这样创建一个MovieCasts集合:

_id
movieId (this is _id from Contents collection, above)
castId (this is _id from Casts collection, below)

类型转换

_id
name
age
etc.

供应商集合

物品收藏

 db.items.aggregate([ { $match: {"item_id":{$eq:"I001"}} }, { $lookup:{ from:"vendor", localField:"vendor_id", foreignField:"vendor_id", as:"vendor_details" } }, { $unwind:"$vendor_details" }, { $project:{ "_id":0, "vendor_id":0, "vendor_details.vendor_company_description":0, "vendor_details._id":0, "vendor_details.country":0, "vendor_details.city":0, "vendor_details.website":0 } } ]); 

产量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM