Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stringify a BSON object inside of a MongoDB map function?

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
like image 718
Seamus Abshere Avatar asked Apr 19 '11 23:04

Seamus Abshere


1 Answers

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)

like image 153
Thomas Broyer Avatar answered Sep 28 '22 15:09

Thomas Broyer