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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:39:49+00:00 2026-05-21T17:39:49+00:00

I have documents with field xyz containing { term: puppies, page: { skip: 1,

  • 0

I have documents with field xyz containing

{ term: "puppies", page: { skip: 1, per_page: 20 } } // not useful as a composite key...
{ page: { skip: 1, per_page: 20 }, term: "puppies" } // different order, same contents

For the sake of determining the “top” values in xyz, I want to map them all to something like

emit('term="puppies",page={ skip: 1, per_page: 20 }', 1); // composite key

but I can’t get the embedded objects into a meaningful strings:

emit('term="puppies",page=[object bson_object]', 1); // not useful

Any suggestions for a function to use instead of toString()?

# return the top <num> values of <field> based on a query <selector>
#
# example: top(10, :xyz, {}, {})
def top(num, field, selector, opts = {})
  m = ::BSON::Code.new <<-EOS
    function() {

      var keys = [];

      for (var key in this.#{field}) {
        keys.push(key);
      }

      keys.sort ();

      var sortedKeyValuePairs = [];

      for (i in keys) {
        var key = keys[i];
        var value = this.#{field}[key];

        if (value.constructor.name == 'String') {
          var stringifiedValue = value;
        } else if (value.constructor.name == 'bson_object') {
          // this just says "[object bson_object]" which is not useful
          var stringifiedValue = value.toString();
        } else {
          var stringifiedValue = value.toString();
        }

        sortedKeyValuePairs.push([key, stringifiedValue].join('='));
      }

      // hopefully we'll end up with something like
      // emit("term=puppies,page={skip:1, per_page:20}")
      // instead of
      // emit("term=puppies,page=[object bson_object]")
      emit(sortedKeyValuePairs.join(','), 1);
    }
  EOS
  r = ::BSON::Code.new <<-EOS
    function(k, vals) {
      var sum=0;
      for (var i in vals) sum += vals[i];
      return sum;
    }
  EOS
  docs = []
  collection.map_reduce(m, r, opts.merge(:query => selector)).find({}, :limit => num, :sort => [['value', ::Mongo::DESCENDING]]).each do |doc|
    docs.push doc
  end
  docs
end
  • 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-05-21T17:39:49+00:00Added an answer on May 21, 2026 at 5:39 pm

    Given that MongoDB uses SpiderMonkey as its internal JS engine, can’t you use JSON.stringify (will work even if/when MongoDB switches to V8) or SpiderMonkey’s non-standard toSource method?

    (sorry, can’t try it ATM to confirm it’d work)

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

Sidebar

Related Questions

My indexed documents have a field containing a pipe-delimited set of ids: a845497737704e8ab439dd410e7f1328| 0a2d7192f75148cca89b6df58fcf2e54|
I have a Lucene index that contains documents that have a type field, this
I'm using Solr for search. I have documents that have an integer field popularity
I have xml documents in a database field. The xml documents have no whitespace
I have a unique ID field of type int in my documents. I'm doing
I have a database of person documents. Each has a field named photos, which
I have several documents in a MongoDB Collection, with a field 'name' (which is
Solr can show the query score of each documents by field 'score'. I have
For example, I have collection with documents, where documents can have field url (but
I have field codes set up in some documents which will display the current

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.