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

Mongo shell throws error with update while connected to multiple DBs.

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • None
    • JavaScript
    • None
    • ALL

    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
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            anindyaju99 Anindya Chakraborty
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: