title: Node.js mongodb: db.collection is not a function
body: <p>With typescript i want use mongo db in this way:</p>
  
  <pre><code>import { config } from '../config';
  import { MongoClient } from 'mongodb';
  
  MongoClient.connect(config.mdb_uri, (err, db) =&gt; {
      if ( err ){
         console.log(err);
         return;
      }
  
      db.collection('mycollection').updateOne( 
        { '_id': 'x' },
        { $set: { 'info': 'OK' } },
        { upsert: true }
      );
  });
  </code></pre>
  
  <p>but this code cause an exception into a docker container:</p>
  
  <blockquote>
    <p>TypeError: db.collection is not a function</p>
  </blockquote>
  
  <p>i have added a <code>console.log(db)</code> and the result is a <code>MongoClient</code> object:</p>
  
  <pre><code>MongoClient {
    domain: null,
    _events: {},
    _eventsCount: 0,
    _maxListeners: undefined,
    s:
     { url: 'mongodb://database:27017/mydb',
       options:
        { socketOptions: {},
          read_preference_tags: null,
          readPreference: [Object],
          dbName: 'dashboard',
          servers: [Array],
          server_options: [Object],
          db_options: [Object],
          rs_options: [Object],
          mongos_options: [Object],
          socketTimeoutMS: 360000,
          connectTimeoutMS: 30000,
          promiseLibrary: [Function: Promise] },
       promiseLibrary: [Function: Promise],
       dbCache: {},
       sessions: [] },
    topology:
     Server {
       domain: null,
       _events:
        { serverOpening: [Function],
          serverDescriptionChanged: [Function],
          serverHeartbeatStarted: [Function],
          serverHeartbeatSucceeded: [Function],
          serverHeartbeatFailed: [Function],
          serverClosed: [Function],
          topologyOpening: [Function],
          topologyClosed: [Function],
          topologyDescriptionChanged: [Function],
          joined: [Function],
          left: [Function],
          ping: [Function],
          ha: [Function],
          authenticated: [Function],
          error: [Function],
          timeout: [Function],
          close: [Function],
          parseError: [Function],
          open: [Object],
          fullsetup: [Object],
          all: [Object],
          reconnect: [Function] },
       _eventsCount: 22,
       _maxListeners: undefined,
       clientInfo:
        { driver: [Object],
          os: [Object],
          platform: 'Node.js v8.4.0, LE' },
       s:
        { coreTopology: [Object],
          sCapabilities: null,
          clonedOptions: [Object],
          reconnect: true,
          emitError: true,
          poolSize: 5,
          storeOptions: [Object],
          store: [Object],
          host: 'database',
          port: 27017,
          options: [Object],
          sessionPool: [Object],
          promiseLibrary: [Function: Promise] } } }
  </code></pre>
  
  <p>from package.json i have installed</p>
  
  <pre><code>"dependencies": {
    ...
    "mongodb": "^3.0.0-rc0",
    ...
  },
  "devDependencies": {
    ...
    "@types/mongodb": "^2.2.16",
    ...
  }
  </code></pre>
  
  <p>from my Dockerfile i install only the production dependencies:</p>
  
  <pre><code>RUN npm install --only=production
  CMD ["npm", "start"]
  </code></pre>
  
  <p>npm start, run the compiled *.ts </p>
  
  <pre><code>"scripts": {
    ...
    "start": "node build/main.js",
    ...
   }
  </code></pre>
  
  <p>the build is produced with</p>
  
  <pre><code> "build": "tsc -p tsconfig.release.json"
  </code></pre>
  
  <p>tsconfig.release.json is</p>
  
  <pre><code>{
    "extends": "./tsconfig.json",
    "compilerOptions": {
      "declaration": false,
      "removeComments": true,
      "jsx": "react"
    },
    "include": [
      "src/**/*"
    ]
  }
  </code></pre>
  
  <p>and tsconfig.json is</p>
  
  <pre><code>{
    "compilerOptions": {
      "target": "es6",
      "module": "commonjs",
      "moduleResolution": "node",
      "allowSyntheticDefaultImports": true,
      "jsx": "preserve",
      "allowJs": true,
      "importHelpers": true,
      "alwaysStrict": true,
      "sourceMap": true,
      "forceConsistentCasingInFileNames": true,
      "noFallthroughCasesInSwitch": true,
      "noImplicitReturns": true,
      "noUnusedLocals": true,
      "noUnusedParameters": false,
      "noImplicitAny": false,
      "noImplicitThis": false,
      "strictNullChecks": false,
      "declaration": true,
      "outDir": "build",
      "rootDir": "src",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true
    },
    "exclude": [
      "node_modules"
    ],
    "include": [
      "src/**/*",
      "__tests__/**/*"
    ]
  }
  </code></pre>
  
  <p>what i'm doing wrong?</p>
  
  <p><strong>SOLVED!!</strong></p>
  
  <p>i have installed mongodb 2.2.0 and now works, see <a href="https://stackoverflow.com/a/47662979/2936170">https://stackoverflow.com/a/47662979/2936170</a></p>