title: Memory issue with mongo in node
body: <p>I am facing memory issues with my node app. Took some heapdumps and saw a lot of mongo objects being held in the memory which is causing the node app to run out of memory.</p>
  
  <p>I have the following setup for my app.</p>
  
  <p>MongoDB 3.4.13</p>
  
  <p>Mongoose 4.11.10 (tried 4.13.11 and 5.0.7 also)</p>
  
  <p>Node 8.9.4</p>
  
  <p>config.js</p>
  
  <pre><code>const clientUID = require('./env').clientUID;
  
  module.exports = {
    // Secret key for JWT signing and encryption
    secret: 'mysecret',
    // Database connection information
    database: `mongodb://localhost:27017/app_${clientUID}`,
    // Setting port for server
    port: process.env.PORT || 3000,
  }
  </code></pre>
  
  <p>I have several models in the app. Every model is defined in the following manner (just listing one of the models here):</p>
  
  <p>models/card.js</p>
  
  <pre><code>const mongoose = require('mongoose');
  const Schema = mongoose.Schema;
  
  const CardSchema = new Schema({
    name: {
      type: String,
      unique: true,
      required: true
    },
      macId: {
      type: String,
      unique: true,
      required: true
    },
    cardTypeId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'CardType',
      required: true
    },
  },
  {
    timestamps: true
  });
  
  module.exports = mongoose.model('Card', CardSchema);
  </code></pre>
  
  <p>In the app I require the model and perform some actions as follows:</p>
  
  <pre><code>const Card = require('./models/card');
  ...require other models
  
  const config = require('./config');
  mongoose.connect(config.database);
  
  function fetchCardByMacId(macId) {
    return Card.findOne({ macId }).lean().exec();
  }
  
  function updateTrackerByMacId(macId, x, y, nodeId) {
    const data = {x, y, lastNodeId: nodeId};
    fetchCardByMacId(macId)
      .then(card =&gt; {
        Tracker.findOneAndUpdate({ cardId: card._id }, data, { upsert: true, new: true }).exec((error, tracker) =&gt; {
          if (error) {
            return console.log('update tracker error', error);
          }
          TrackerHistory.findOne({ trackerId: tracker._id }).exec((err, trackerHistory) =&gt; {
            if (err) {
              return console.log('fetch trackerHistory error', err);
            }
            if (trackerHistory) {
              trackerHistory.trackers.push({ x, y, timestamp: moment().format(), nodeId });
              TrackerHistory.findOneAndUpdate({_id: trackerHistory._id},trackerHistory,(er, trackerHis) =&gt; {
                if (er) {
                  return console.log('trackerHistory change update error', er);
                }
              })
            } else {
              const trackerHistoryNew = new TrackerHistory({
                trackerId: tracker._id,
                trackers: [{ x, y, timestamp: moment().format(), nodeId }]
              });
              trackerHistoryNew.save((er, trackerHis) =&gt; {
                if (er) {
                  return console.log('trackerHistory create error', er);
                }
              });
            }
          });
        });
      }).catch(error =&gt; {
        console.log('updateTrackerByMacId error', error);
      });
  }
  </code></pre>
  
  <p>Like this there are many other functions that read and update data.</p>
  
  <p>Every 5 seconds I get new data that needs to be inserted into the db (not more than few 100kbs) and some of the old db data also gets updated based on this new data (seems like fairly straight forward db ops...read, manipulate and update back).</p>
  
  <p>From the index.js I spawn 2 child processes that take the load of processing this new data and updating the db based on the business logic. When new data is received in the index.js using event listeners, I send it to child process 1 to insert/update the db. child process 2 runs on a 10s timer to read this updated data and then do some further updates to the db.</p>
  
  <p>Running this on my local macbook pro is no issue (logging heap memory being used never goes above 40-50mb). When i load it on a DO Ubuntu 16.04 server (4GB /2 CPUs) I am facing memory issues. The child processes are exiting after hitting the memory threshold for the process (~1.5gb) which seems very odd to me. 
  I also tried to do this using docker containers and see the same results. on the mac it runs without issues but on the server it is eating up memory.
  Generating heapdumps shows a lot of mongo objects in the heap.</p>
  
  <p>I would like some help in understanding what I am doing wrong here and what is the issue with mongo eating up this much memory on the server.</p>