Javascript Node.js mongodb:db.collection不是函数

Javascript Node.js mongodb:db.collection不是函数,javascript,node.js,mongodb,docker,Javascript,Node.js,Mongodb,Docker,对于typescript,我希望以以下方式使用mongo db: import { config } from '../config'; import { MongoClient } from 'mongodb'; MongoClient.connect(config.mdb_uri, (err, db) => { if ( err ){ console.log(err); return; } db.collection('mycol

对于typescript,我希望以以下方式使用mongo db:

import { config } from '../config';
import { MongoClient } from 'mongodb';

MongoClient.connect(config.mdb_uri, (err, db) => {
    if ( err ){
       console.log(err);
       return;
    }

    db.collection('mycollection').updateOne( 
      { '_id': 'x' },
      { $set: { 'info': 'OK' } },
      { upsert: true }
    );
});
但此代码会导致docker容器出现异常:

TypeError:db.collection不是函数

我添加了一个
console.log(db)
,结果是一个
MongoClient
对象:

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] } } }
从我安装的package.json

"dependencies": {
  ...
  "mongodb": "^3.0.0-rc0",
  ...
},
"devDependencies": {
  ...
  "@types/mongodb": "^2.2.16",
  ...
}
从Dockerfile中,我只安装生产依赖项:

RUN npm install --only=production
CMD ["npm", "start"]
npm启动,运行编译的*.ts

"scripts": {
  ...
  "start": "node build/main.js",
  ...
 }
这个构建是用

 "build": "tsc -p tsconfig.release.json"
tsconfig.release.json是

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "declaration": false,
    "removeComments": true,
    "jsx": "react"
  },
  "include": [
    "src/**/*"
  ]
}
而tsconfig.json是

{
  "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__/**/*"
  ]
}
我做错了什么

已解决


我已经安装了mongodb 2.2.0,现在可以使用了,请参见

答案是。在这种情况下,亚曼克斯的复制品不是正确的复制品。也救了我的命。