Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8401939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:55:47+00:00 2026-06-09T21:55:47+00:00

For example, there is a collection like this: {market: ‘SH’, code: ‘000001’, date: ‘2012-01-01’,

  • 0

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}
…

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′}]

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:55:49+00:00Added an answer on June 9, 2026 at 9:55 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to explain this by example: Is there a best practice or preference
[This is a simplified example] I have a collection (myCollection) in which exists three
I was wondering if there was a more elegant way than this example to
I have a model with an object that contains a collection like this: namespace
Given the following code from a Microsoft example: public class EngineMeasurementCollection : Collection<EngineMeasurement> {
The example I see everywhere for MVC-style routing is something like this: void Application_Start(object
MongoDB's PHP library allows me to connect to a collection like this ( from
Are there any .NET frameworks for collecting data similar to Google Analytics, for example
Lets say for example there are 5 records shown on screen. I want the
On the Groovy example page there is an example of how to use Groovy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.