For example, there is a collection like this:
{market: \'SH\', code: \'000001\', date: \'2012-01-01\', price: 1000}
{market: \'SZ\', code:
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.