mongodb 如何使用多个键有效地执行“不同”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11973725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:47:57  来源:igfitidea点击:

How to efficiently perform "distinct" with multiple keys?

mongodb

提问by user805627

For example, there is a collection like this:

例如,有一个这样的集合:

{market: 'SH', code: '000001', date: '2012-01-01', price: 1000}
{market: 'SZ', code: '000001', date: '2012-01-01', price: 1000}
{market: 'SH', code: '000001', date: '2012-01-02', price: 1000}
{market: 'SZ', code: '000001', date: '2012-01-02', price: 1000}
{market: 'SH', code: '000002', date: '2012-01-03',price: 1000}
...

{市场:'SH',代码:'000001',日期:'2012-01-01',价格:1000}
{市场:'SZ',代码:'000001',日期:'2012-01-01',价格:1000}
{市场:'SH',代码:'000001',日期:'2012-01-02',价格:1000}
{市场:'SZ',代码:'000001',日期:'2012-01 -02',价格:1000}
{市场:'SH',代码:'000002',日期:'2012-01-03',价格:1000}
...

This collection contains tens of millions documents.

该集合包含数千万个文档。

I want to call distinct with two keys:

我想用两个键调用不同的:

collection.distinct('market', 'code');

and get result :

并得到结果:

[{market: 'SH', code:'000001'}, {market: 'SZ', code:'000001'}, {market: 'SH', code:'000002'}]

[{市场:'SH',代码:'000001'},{市场:'SZ',代码:'000001'},{市场:'SH',代码:'000002'}]

As native distinct command accept only one key, I try to implement it by using map-reduce. But map-reduce is far too slow to native distinct. In my one-key distinct test, map-reduce spend about ten times longer than native distinct.
Is there a efficient way to implement multikey distinct?

由于本机不同命令只接受一个键,我尝试使用 map-reduce 来实现它。但是 map-reduce 对原生不同来说太慢了。在我的一键 distinct 测试中,map-reduce 花费的时间大约是原生 distinct 的十倍。
有没有一种有效的方法来实现多键不同?

回答by William Z

If you are willing to wait for the upcoming 2.2 release of MongoDB, you can run this query efficiently using the aggregation framework:

如果您愿意等待即将发布的 MongoDB 2.2 版本,您可以使用聚合框架高效地运行此查询:

collection = db.tb;
result = collection.aggregate( 
            [
                {"$group": { "_id": { market: "$market", code: "$code" } } }
            ]
        );
printjson(result);

On a million-record collection on my test machine, this ran in 4 seconds, while the map/reduce version took over a minute.

在我的测试机器上的百万条记录集合中,这运行了 4 秒,而 map/reduce 版本需要一分钟多。