[SERVER-21664] Mongo shell throws error with update while connected to multiple DBs. Created: 25/Nov/15  Updated: 25/Nov/15  Resolved: 25/Nov/15

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

Type: Bug Priority: Major - P3
Reporter: Anindya Chakraborty Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

The details are at http://stackoverflow.com/questions/33873213/mongo-shell-update-causes-typeerror-object-is-not-a-function-at-mongo-getdb

var config = {
  marketingDB: 'localhost:27017/marketing',
  feedbackDB: 'localhost:27017/feedback',
  teleporterDB: 'localhost:27017/teleporter',
  batchSz: 1000
};
 
var DB = {
  marketingDB: connect(config.marketingDB),
  feedbackDB: connect(config.feedbackDB),
  teleporterDB: connect(config.teleporterDB)
};
 
function Datasegment() {
  var db = DB.teleporterDB;
  var datasegmentCollectionObjects = {};
 
  function init() {
    var collectionNames = db.getCollectionNames();
    for (var i in collectionNames) {
      var datasegmentName = collectionNames[i];
      datasegmentCollectionObjects[datasegmentName] = db[datasegmentName]
    }
  };
 
  this.getCollection = function(dataSegmentName) {
    return datasegmentCollectionObjects[dataSegmentName];
  };
 
  init();
};
 
function Template() {
  var db = DB.marketingDB;
 
  this.findAllTemplateDataSnapshotMap = function() {
    var results = {};
    db.template.find().forEach(
      function(rec){
        if(rec.data_snapshot_id) {
          results[rec._id] = rec.data_snapshot_id;
        }
      });
 
    return results;
  };
}
 
function Runbook() {
  var db = DB.feedbackDB;
  var datasegment = new Datasegment();
  var template = new Template();
 
  this.merge = function() {
    var templateIdDatasegmentNameMap = template.findAllTemplateDataSnapshotMap();
    printjson(templateIdDatasegmentNameMap);
    var jobDataSegmentMapping = getJobIdDataSegmentMapping(templateIdDatasegmentNameMap);
    printjson(jobDataSegmentMapping);
    mergeAllTeleporterDataToRunbook(jobDataSegmentMapping);
  };
 
  function getRunbookCollection(jobId) {
    return db[jobId];
  }
 
  function getRunbookCollectionWrite(jobId) {
    return writeDB[jobId];
  }
 
  function getRequestRowIndexInDataSegment(channel, requestId) {
        var lastPos = requestId.lastIndexOf("_");
        var idx = 0;
 
        if (channel == 'push') {
            var usualReqId = requestId.substring(lastPos + 1, requestId.length - 1);
            var lastToLastPos = usualReqId.lastIndexOf("_");
            idx = usualReqId.substring(lastToLastPos + 1, requestId.length - 1);
        } else {
            idx = requestId.substring(lastPos + 1, requestId.length - 1);
        }
        return idx;
  }
 
  function getRequestRowInDataSegment(datasegmentCollection, index) {
        return datasegmentCollection.find().skip(index).limit(1).next()
  }
 
  function mergeTeleporterDataToRunbook(jobId, runbookCollection, datasegmentCollection ) {
        if (!runbookCollection || !datasegmentCollection) {
          return; // fall back to do nothing
        }
 
        try {
        var sz = runbookCollection.find().count();
        var offset = 0;
 
        while (offset <= sz) {
          var batchData = [];
          runbookCollection.find().skip(offset).limit(config.batchSz).forEach(function(ref) {
            if (ref.user_id) {
                // this is already populated. No need
                throw 100;
            }
 
            var index = getRequestRowIndexInDataSegment(ref.channel, ref.request_id);
            var datasegmentRow = getRequestRowInDataSegment(datasegmentCollection, index);
            var info = {};
            if (datasegmentRow.user_id) {
              info['user_id'] = datasegmentRow.user_id;
            }
 
            if(ref.channel === 'sms') {
              if (datasegmentRow.phone) {
                info['phone'] = datasegmentRow.phone;
              }
            }
 
            if(ref.channel === 'email') {
              if (datasegmentRow.email) {
                info['email'] = datasegmentRow.email;
              }
            }
 
            //runbookCollection.update({request_id : ref.request_id}, { $set : info });
            batchData.push({request_id: ref.request_id, info: info});
          }
          );
 
          for (var i in batchData) {
            var ref = batchData[i];
            // this is the line with error
            runbookCollection.update({request_id : ref.request_id}, { $set : ref.info });
          }
          offset = offset + batchData.length;
          batchData = [];
        }
        } catch (err) {
            if (err !== 100) throw err;
        }
 
        printjson("Done with " + jobId);
  }
 
 
  function mergeAllTeleporterDataToRunbook(jobDataSegmentMapping) {
    //printjson(jobDataSegmentMapping);
    for (var jobId in jobDataSegmentMapping) {
      var dataSegmentName = jobDataSegmentMapping[jobId];
      var runbookCollection = getRunbookCollection(jobId);
      var datasegmentCollection = datasegment.getCollection(dataSegmentName);
      mergeTeleporterDataToRunbook(jobId, runbookCollection,
                  datasegmentCollection);
    }
  }

Error:

2015-11-23T20:39:12.674+0530 E QUERY    TypeError: object is not a function
    at Mongo.getDB (src/mongo/shell/mongo.js:41:12)
    at Mongo.hasWriteCommands (src/mongo/shell/mongo.js:204:29)
    at Mongo.writeMode (src/mongo/shell/mongo.js:244:15)
    at DBCollection.update (src/mongo/shell/collection.js:443:26)
    at mergeTeleporterDataToRunbook (copy_user_info.mongo:154:31)
    at mergeAllTeleporterDataToRunbook (copy_user_info.mongo:173:7)
    at Runbook.merge (copy_user_info.mongo:66:5)
    at copy_user_info.mongo:190:15 at copy_user_info.mongo:160
failed to load: copy_user_info.mongo



 Comments   
Comment by Kamran K. [ 25/Nov/15 ]

anindyaju99, your declaration of 'DB' on line 10 is overriding the global DB variable, which leads to the TypeError (and will cause other issues in various shell methods).

To avoid the problem, rename DB to something like myDBs instead.

Hope that helps.

Comment by Anindya Chakraborty [ 25/Nov/15 ]

Mongo 3.0.6. I could reproduce this with both replica set and sharding setups. Stand alone i didn't try.

Comment by Ramon Fernandez Marina [ 25/Nov/15 ]

What version of MongoDB are you using? What kind of setup do you have (stand-alone, replica set, sharded cluster)?

Thanks,
Ramón.

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