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 7706085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:01:43+00:00 2026-06-01T00:01:43+00:00

Mongo docs state: The Mongo multikey feature can automatically index arrays of values. That’s

  • 0

Mongo docs state:

The Mongo multikey feature can automatically index arrays of values.

That’s nice. But how about sorting based on multikeys? More specifically, how to sort a collection according to array match percentage?

For example, I have a pattern [ 'fruit', 'citrus' ] and a collection, that looks like this:

{
    title: 'Apples',
    tags: [ 'fruit' ]
},

{
    title: 'Oranges',
    tags: [ 'fruit', 'citrus' ]
},

{
    title: 'Potato',
    tags: [ 'vegetable' ]
}

Now, I want to sort the collection according to match percentage of each entry to the tags pattern. Oranges must come first, apples second and potatoes last.

What’s the most efficient and easy way to do it?

  • 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-01T00:01:46+00:00Added an answer on June 1, 2026 at 12:01 am

    As of MongoDB 2.1 a similar computation can be done using the aggregation framework. The syntax is something like

    db.fruits.aggregate(
         {$match : {tags : {$in : ["fruit", "citrus"]}}}, 
         {$unwind : "$tags"}, 
         {$group : {_id : "$title", numTagMatches : {$sum : 1}}}, 
         {$sort : {numTagMatches : -1}} )
    

    which returns

     {
       "_id" : "Oranges",
       "numTagMatches" : 2
     },
     {
       "_id" : "Apples",
       "numTagMatches" : 1
     }
    

    This should be much faster than the map-reduce method for two reasons. First because the implementation is native C++ rather than javascript. Second, because “$match” will filter out the items which don’t match at all (if this is not what you want, you can leave out the “$match” part, and change the “$sum” part to be either 1 or 0 depending on if the tag is equal to “fruit” or “citrus” or neither).

    The only caveat here is that mongo 2.1 isn’t recommended for production yet. If you’re running in production you’ll need to wait for 2.2. But if you’re just experimenting on your own you can play around with 2.1, as the aggregation framework should be more performant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Mongo my understanding is that you can have databases and collections. I'm working
Well guys, I'm trying to get that MongoDB feature working for hours: http://www.mongodb.org/display/DOCS/Http+Interface I
In the docs, it says that by default, Mongo treats it as lat/long: By
I've found that Mongoose syntax isn't very well documented yet where as Mongo docs
I can't seem to connect to Mongo DB, which I've installed as a Windows
Mongo has the nice operator $slice, which let's you only retrieve a sub sets
If I have a mongo instance running, how can I check what port numbers
How can I run aggregate, min, max, sum and friends on embedded docs? For
The docs http://msdn.microsoft.com/en-us/library/ms597043%28v=VS.96%29.aspx state the System.Windows.Clipboard should be available in Windows Phone 7.1. I
Being relatively new to mongo, I read in the mongo manual about Optimizing Object

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.