[SERVER-11900] "subDoc.constructor === Object" always returns false Created: 29/Nov/13  Updated: 27/Oct/21  Resolved: 27/Oct/21

Status: Closed
Project: Core Server
Component/s: JavaScript
Affects Version/s: 2.4.6
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Nicolas Fouché Assignee: Geert Bosch
Resolution: Won't Do Votes: 0
Labels: nc
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

CentOS 6.x. 3 shards, with replica sets.


Operating System: ALL
Participants:

 Description   

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 ?


Generated at Thu Feb 08 03:27:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.