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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:39:25+00:00 2026-06-01T14:39:25+00:00

I have a document structure {‘text’: ‘here is text’, ‘count’ : 13, ‘somefield’: value}

  • 0

I have a document structure {'text': 'here is text', 'count' : 13, 'somefield': value}

Collection has some thousands of record, and text key value may be repeated many time, I want to find distinct text with highest count value,along with that whole document should be returned , I am able to sort them in descending order.

distinct returns unique value in a list.

I want to use all three functions and document has to be returned, I am still learning and not covered mapreduce.

  • 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-01T14:39:26+00:00Added an answer on June 1, 2026 at 2:39 pm

    Can you please clarify exactly what you would like to do? Do you want to return documents with unique “text” values with the highest “count” value?

    For example, given the collection:

    > db.text.find({}, {_id:0})
    { "text" : "here is text", "count" : 13, "somefield" : "value" }
    { "text" : "here is text", "count" : 12, "somefield" : "value" }
    { "text" : "here is text", "count" : 10, "somefield" : "value" }
    { "text" : "other text", "count" : 4, "somefield" : "value" }
    { "text" : "other text", "count" : 3, "somefield" : "value" }
    { "text" : "other text", "count" : 2, "somefield" : "value" }
    >
    (I have omitted _id values for brevity)
    

    Would you like to return only the documents that contain unique text with the highest ‘count’ value?

    { "text" : "here is text", "count" : 13, "somefield" : "value" }
    

    and

    { "text" : "other text", "count" : 4, "somefield" : "value" }
    

    One way to do this is with the $group and $max functions in the new aggregation framework. The documentation on $group may be found here:
    http://docs.mongodb.org/manual/aggregation/

    > db.text.aggregate({$group : {_id:"$text", "maxCount":{$max:"$count"}}})
    {
        "result" : [
            {
                "_id" : "other text",
                "maxCount" : 4
            },
            {
                "_id" : "here is text",
                "maxCount" : 13
            }
        ],
        "ok" : 1
    }
    

    As you can see, the above does not return the original documents. If the original documents are desired, a query may then be done to find documents matching the unique text and count values.

    As an alternative, you can first do run the ‘distinct’ command to return an array of all the distinct values and then run a query for each value with sort and limit to return the document with the highest value of ‘count’. The sort() and limit() methods are explained in the “Cursor Methods” section of the “Advanced Queries” documentation:
    http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-CursorMethods

    > var values = db.runCommand({distinct:"text", key:"text"}).values
    > values
    [ "here is text", "other text" ]
    > for(v in values){var c = db.text.find({"text":values[v]}).sort({count:-1}).limit(1); c.forEach(printjson);}
    {
        "_id" : ObjectId("4f7b50b2df77a5e0fd4ccbf1"),
        "text" : "here is text",
        "count" : 13,
        "somefield" : "value"
    }
    {
        "_id" : ObjectId("4f7b50b2df77a5e0fd4ccbf4"),
        "text" : "other text",
        "count" : 4,
        "somefield" : "value"
    }
    

    It is unclear if this is exactly what you are trying to do, but I hope that it will at least give you some ideas to get started. If I have misunderstood, please explain in more detail the exact operation that you would like to perform, and hopefully I or another member of the Community will be able to help you out. Thanks.

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

Sidebar

Related Questions

I have a Mongo collection where each document has a set of unique embedded
I have a given XML document (structure can not be changed) and want to
i have a structure of document in mongodb as below { _id: some_id, key1:
I have this structure in my html document: <p> <em>You</em> began the evening well,
If I have a table structure like this: Transaction [TransID, ...] Document [DocID, TransID,
I have an element that contains two span tags that each contain some text.
I have a document based on a xml structure that I have stored in
I have an XML document with this structure: <Fruits> <Fruit> <Code>1</Code> <Name>Apple</Name> </Fruit> </Fruits>
I have a document which has a nested frameset. I need to access one
I have something that looks like the following document structure: public class Document {

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.