Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-11900

"subDoc.constructor === Object" always returns false

    • Type: Icon: Bug Bug
    • Resolution: Won't Do
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.4.6
    • Component/s: JavaScript
    • Environment:
      CentOS 6.x. 3 shards, with replica sets.
    • ALL

      I have a javascript function which merges two objects recursively. I call it in a reduce() to merge documents and add their numbers. This function works fine on node.js, and in the Chrome console. (so it's not a v8 problem)

            merge_and_sum_recursive_in_place = function(obj1, obj2) {
              var k, v;
              if (obj1 == null) {
                return obj2;
              }
              if (obj2 == null) {
                return obj1;
              }
              for (k in obj2) {
                v = obj2[k];
                if (v.constructor === Object) // THIS NEVER RETURNS TRUE
                // if (typeof(v) === 'object') { // THIS WORKS 
                  obj1[k] = merge_and_sum_recursive_in_place(obj1[k], v);
                } else {
                  if (obj1[k] != null) {
                    if (v.constructor === Number) { // THIS WORKS
                      obj1[k] += v;
                    } else {
                      obj1[k] = v;
                    }
                  } else {
                    obj1[k] = v;
                  }
                }
              }
              return obj1;
            };
      

      All the numbers in the subdocuments are never added if I use "v.constructor === Object", but it works fine if I use "typeof(v) === 'object'".

      Note that "v.constructor === Number" works perfectly.

      Perhaps subdocument objects parsed from BSON are not "real" javascript "Object"s ?

            Assignee:
            geert.bosch@mongodb.com Geert Bosch
            Reporter:
            nicolas_ Nicolas Fouché
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: