How to efficiently perform “distinct” with multiple keys?

后端 未结 1 1585
执念已碎
执念已碎 2020-11-27 16:26

For example, there is a collection like this:

{market: \'SH\', code: \'000001\', date: \'2012-01-01\', price: 1000}
{market: \'SZ\', code:

相关标签:
1条回答
  • 2020-11-27 16:59

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

    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.

    0 讨论(0)
提交回复
热议问题