简体   繁体   English

mongodb中的合并聚合

[英]Merge aggregation in mongodb

I need to write a code that solves the following question:我需要编写一个代码来解决以下问题:

On the actors collection, show the actors whose career has been the longest.演员集上,展示职业生涯最长的演员。

I start from the following .json file: https://drive.google.com/file/d/1VKuhce2ofjLjYEoND_Fz2wqcYHAf6kkZ/view我从以下 .json 文件开始: https : //drive.google.com/file/d/1VKuhce2ofjLjYEoND_Fz2wqcYHAf6kkZ/view

//unwind
    db.getCollection("Tarea").find({})
    fase1 = { $unwind: "$cast"}
    etapas = [fase1]
    db.Tarea.aggregate( etapas )

//out
    fase1 = { $unwind: "$cast" }
    query2 = { "_id": 0 }
    fase2 = { $project: query2 }
    fase3 = { $out: "actors" }
    etapas = [ fase1, fase2, fase3 ]
    db.Tarea.aggregate( etapas )

So I created the actors collection.所以我创建了演员集合。 And now I need to know how long the actors have been active.现在我需要知道演员们活跃了多长时间。 I suppose it could be done by grouping the actors in the _id column and creating a new column that subtracts:我想可以通过将 _id 列中的演员分组并创建一个减去以下内容的新列来完成:

'Most recent movie year - First movie year' “最近的电影年 - 第一个电影年”

to later order that same column from highest to lowest, thus obtaining the "career time" of each actor.稍后将同一列从高到低排序,从而获得每个演员的“职业时间”。

I don't know if there is another simpler way to do it.不知道有没有其他更简单的方法。

Thanks to everyone beforehand.先谢谢大家了。

There are Many Many ways you could do this, with that said - I would use this pipeline:有很多方法可以做到这一点,也就是说 - 我会使用这个管道:

db.collection("Tarea").aggregate([
    {
        $unwind: "$cast"
    },
    {
        $group: {
            _id: "$cast",
            last: {$max: "$year"},
            first: {$min: "$year"}
        }
    },
    {
        $project: {
            actor: "$_id",
            careerLength: {$subtract: ["$last", "$first"]}
        }
    },
    {
        $sort: {
            careerLength: -1
        }
    }
]);

not sure about the coding language of the op but here's ac# version if anybody's interested:不确定操作的编码语言,但如果有人感兴趣,这里是 ac# 版本:

using MongoDB.Entities;
using MongoDB.Entities.Core;
using System.Linq;

namespace StackOverflow
{
    public class movie : Entity
    {
        public string title { get; set; }
        public int year { get; set; }
        public string[] cast { get; set; }
    }

    public class Program
    {
        private static void Main(string[] args)
        {
            new DB("test", "localhost");

            var res = DB.Queryable<movie>() // for official driver use: collection.AsQueryable()

                        .SelectMany(m => m.cast,
                                   (m, a) => new { actor = a, m.year })

                        .GroupBy(x => x.actor)

                        .Select(g => new
                        {
                            actor = g.Key,
                            careerLength = g.Max(x => x.year) - g.Min(x => x.year)
                        })

                        .OrderByDescending(x => x.careerLength)

                        .Take(100)

                        .ToArray();
        }
    }
}

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

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