MapReduce将重复值分组到一个数组中

时间:2018-11-13 05:54:01

标签: json mongodb mapreduce

我是Mongodb的新手,我一直在尝试Mapreduce函数。我在MongoDb中有一个具有以下结构的普通集合:

/* 1 */

{
    "_id" : ObjectId("5be931157dd6fcc118e7c686"),
    "title" : "MongoDB: The Definitive Guide",
    "published" : "2013-05-23",
    "authors" : [ 
        {
            "firstName" : "Kristina",
            "lastName" : "Chodorow"
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5be931257dd6fcc118e7c687"),
    "title" : "MongoDB Applied Design Patterns",
    "published" : "2013-03-19",
    "authors" : [ 
        {
            "firstName" : "Rick",
            "lastName" : "Copeland"
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("5be931357dd6fcc118e7c688"),
    "title" : "MongoDB in Action",
    "published" : "2011-12-16",
    "authors" : [ 
        {
            "firstName" : "Kyle",
            "lastName" : "Banker"
        }
    ]
}

/* 5 */
{
    "_id" : ObjectId("5be931547dd6fcc118e7c68a"),
    "title" : "Scaling MongoDB",
    "published" : "2011-03-07",
    "authors" : [ 
        {
            "firstName" : "Kristina",
            "lastName" : "Chodorow"
        }
    ]
}

/* 6 */
{
    "_id" : ObjectId("5be931677dd6fcc118e7c68c"),
    "title" : "50 Tips and Tricks for MongoDB Developers",
    "published" : "2011-05-06",
    "authors" : [ 
        {
            "firstName" : "Kristina",
            "lastName" : "Chodorow"
        }
    ]
}

我想根据作者姓名对数据进行分组,并将各个作者的书名排列在一个数组中。我当时正在研究mongodb的mapreduce函数,但是似乎无法提出解决方案。

预期结果

{
    "_id" : {
        "firstName" : "Kristina",
        "lastName" : "Chodorow"
    }
    "title":"[Scaling MongoDB,MongoDB: The Definitive Guide,MongoDB: The Definitive Guide]"
}

0 个答案:

没有答案