MongoDB 数据库连接无法在 express 中使用 mongoose

MongoDB database connection is not working in express with mongoose

我正在尝试将 mongoDB 数据库与我的应用程序连接,但无法成功。我遵循了与此相关的另一个答案,但失败了,这就是我在这里问的原因。

这是一个旧项目。它现在需要连接并且 运行 保留旧的软件包版本。该项目处于 运行ning 阶段,现在需要连接 mongodb 数据库

代码示例如下:

package.json

 {
  "name": "...",
  "description": "Full-Stack JavaScript with MongoDB, Express, AngularJS, and Node.js",
  "version": "0.0.1",
  "author": "...",
  "engines": {
    "node": "4.4.4",
    "npm": "3.8.x"
  },
  "scripts": {
    "start": "grunt",
    "debug": "grunt debug",
    "test": "grunt test",
    "postinstall": "bower install --config.interactive=false"
  },
  "dependencies": {
    "async": "~0.9.0",
    "body-parser": "~1.9.0",
    "bower": "~1.3.8",
    "chalk": "~0.5",
    "compression": "~1.2.0",
    "connect-flash": "~0.1.1",
    "connect-mongo": "^1.1.0",
    "consolidate": "~0.10.0",
    "cookie-parser": "~1.3.2",
    "express": "^4.13.4",
    "express-session": "^1.13.0",
    "forever": "~0.11.0",
    "glob": "~4.0.5",
    "gridfs-stream": "^1.1.1",
    "grunt-cli": "~0.1.13",
    "helmet": "~0.5.0",
    "lodash": "^4.12.0",
    "method-override": "~2.3.0",
    "mongoose": "4.4.16",
    "morgan": "~1.4.1",
    "multer": "^1.1.0",
    "nodemailer": "~2.4.1",
    "passport": "~0.2.0",
    "passport-facebook": "~1.0.2",
    "passport-github": "~0.1.5",
    "passport-google-oauth": "~0.1.5",
    "passport-linkedin": "~0.1.3",
    "passport-local": "~1.0.0",
    "passport-twitter": "~1.0.2",
    "paypal-rest-sdk": "^1.6.8",
    "q": "^1.5.1",
    "stripe": "^4.6.0",
    "swig": "~1.4.1",
    "validator": "^5.2.0"
  },
  "devDependencies": {
    "grunt": "^1.0.1",
    "grunt-concurrent": "^2.3.0",
    "grunt-contrib-csslint": "^1.0.0",
    "grunt-contrib-cssmin": "^1.0.1",
    "grunt-contrib-jshint": "^1.0.0",
    "grunt-contrib-uglify": "^1.0.1",
    "grunt-contrib-watch": "^1.0.0",
    "grunt-env": "^0.4.4",
    "grunt-karma": "^1.0.0",
    "grunt-mocha-test": "^0.12.7",
    "grunt-ng-annotate": "^2.0.2",
    "grunt-nodemon": "^0.4.2",
    "karma": "^0.13.22",
    "karma-chrome-launcher": "^1.0.1",
    "karma-coverage": "^1.0.0",
    "karma-firefox-launcher": "^0.1.7",
    "karma-jasmine": "^1.0.2",
    "karma-phantomjs-launcher": "^1.0.0",
    "load-grunt-tasks": "^3.5.0",
    "mocha": "^2.4.5",
    "should": "^8.3.1",
    "supertest": "^1.2.0"
  }
}

server.js

    var db = mongoose.connect('mongodb://<username>:<password>@cluster0.qlvoh.mongodb.net/<dbname>?retryWrites=true&w=majority',   function(err) {
    if (err) {
        console.error(chalk.red('Could not connect to MongoDB!'));
        console.log(chalk.red(err));
    } else {
        console.log('Connection Successful');
    }
}, {useMongoClient: true});

我使用了正确的用户名、密码和数据库名称并在 mongodb-compass 上进行了测试。它在 mongodb-compass 中成功连接。所以用户名和密码确实是正确的。

当我在 uri 字符串中使用 mongodb+srv://... 时,控制台错误类似于;

[nodemon] 1.19.4
    [nodemon] to restart at any time, enter `rs`
    [nodemon] watching dir(s): app/views/**/*.* gruntfile.js server.js config/**/*.js app/**/*.js
    [nodemon] watching extensions: js,html
    [nodemon] starting `node server.js`
    NODE_ENV is not defined! Using default development environment
    Could not connect to MongoDB!
    Error: Invalid mongodb uri. Must begin with "mongodb://"
      Received: ...
      .../node_modules/gridfs-stream/lib/index.js:25
      if (!db) throw new Error('missing db argument\nnew Grid(db, mongo)');
               ^
    
    Error: missing db argument
    new Grid(db, mongo)

所以我在连接字符串中使用 mongodb:// 然后它 returns Could not connect to MongoDB! 如下:

注意到 mongoose 版本是 4.4.16

当我使用 mongoose 版本 5 或更高版本时,returns 来自 node_modules/connect-mongo 的错误如下:

...node_modules/connect-mongo/src/index.js:105
                    throw new Error('Connection strategy not found');
                    ^
    
    Error: Connection strategy not found
        at new MongoStore... 

有人帮我解决这个问题吗?

提前致谢!

我只需从 MongoDB atlas 帐户中选择 node 2.2.12 or later 选项即可成功连接。

字符串uri如下所示:

mongodb://<username>:<password>@cluster0-shard-0000.vnrr0.mongodb.net:27017,cluster0-shard-00-01.vnrr0.mongodb.net:27017,cluster0-shard-00-02.vnrr0.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=atlas-134hkh-shard-0&authSource=admin&retryWrites=true&w=majority