MongoDB:将来自多个集合的数据合并为一个..如何? [英] MongoDB: Combine data from multiple collections into one..how?

查看:21
本文介绍了MongoDB:将来自多个集合的数据合并为一个..如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何(在 MongoDB 中)将来自多个集合的数据合并到一个集合中?

How can I (in MongoDB) combine data from multiple collections into one collection?

我可以使用 map-reduce 吗?如果可以,如何使用?

Can I use map-reduce and if so then how?

由于我是新手,我将不胜感激.

I would greatly appreciate some example as I am a novice.

推荐答案

MongoDB 3.2 现在允许通过 $lookup 聚合阶段.作为一个实际示例,假设您将有关书籍的数据分为两个不同的集合.

MongoDB 3.2 now allows one to combine data from multiple collections into one through the $lookup aggregation stage. As a practical example, lets say that you have data about books split into two different collections.

第一个集合,称为 books,包含以下数据:

First collection, called books, having the following data:

{
    "isbn": "978-3-16-148410-0",
    "title": "Some cool book",
    "author": "John Doe"
}
{
    "isbn": "978-3-16-148999-9",
    "title": "Another awesome book",
    "author": "Jane Roe"
}

第二个集合,称为 books_sales_data,具有以下数据:

And the second collection, called books_selling_data, having the following data:

{
    "_id": ObjectId("56e31bcf76cdf52e541d9d26"),
    "isbn": "978-3-16-148410-0",
    "copies_sold": 12500
}
{
    "_id": ObjectId("56e31ce076cdf52e541d9d28"),
    "isbn": "978-3-16-148999-9",
    "copies_sold": 720050
}
{
    "_id": ObjectId("56e31ce076cdf52e541d9d29"),
    "isbn": "978-3-16-148999-9",
    "copies_sold": 1000
}

要合并两个集合,只需按以下方式使用 $lookup:

To merge both collections is just a matter of using $lookup in the following way:

db.books.aggregate([{
    $lookup: {
            from: "books_selling_data",
            localField: "isbn",
            foreignField: "isbn",
            as: "copies_sold"
        }
}])

经过此聚合后,books 集合将如下所示:

After this aggregation, the books collection will look like the following:

{
    "isbn": "978-3-16-148410-0",
    "title": "Some cool book",
    "author": "John Doe",
    "copies_sold": [
        {
            "_id": ObjectId("56e31bcf76cdf52e541d9d26"),
            "isbn": "978-3-16-148410-0",
            "copies_sold": 12500
        }
    ]
}
{
    "isbn": "978-3-16-148999-9",
    "title": "Another awesome book",
    "author": "Jane Roe",
    "copies_sold": [
        {
            "_id": ObjectId("56e31ce076cdf52e541d9d28"),
            "isbn": "978-3-16-148999-9",
            "copies_sold": 720050
        },
        {
            "_id": ObjectId("56e31ce076cdf52e541d9d28"),
            "isbn": "978-3-16-148999-9",
            "copies_sold": 1000
        }
    ]
}

注意几点很重要:

  1. 来自"集合,在本例中为 books_sales_data,不能分片.
  2. as"字段将是一个数组,如上例所示.
  3. $lookup 阶段上的localField"和foreignField"选项都将是如果它们在各自的集合中不存在($lookup 文档 有一个很好的例子).
  1. The "from" collection, in this case books_selling_data, cannot be sharded.
  2. The "as" field will be an array, as the example above.
  3. Both "localField" and "foreignField" options on the $lookup stage will be treated as null for matching purposes if they don't exist in their respective collections (the $lookup docs has a perfect example about that).

因此,作为结论,如果您想合并两个集合,在这种情况下,有一个包含已售总副本的平面副本sold 字段,您将需要多做一些工作,可能会使用一个中间集合,然后,将 $out 到最终集合.

So, as a conclusion, if you want to consolidate both collections, having, in this case, a flat copies_sold field with the total copies sold, you will have to work a little bit more, probably using an intermediary collection that will, then, be $out to the final collection.

这篇关于MongoDB:将来自多个集合的数据合并为一个..如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆