首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB数据库连接无法与猫鼬一起使用。

MongoDB数据库连接无法与猫鼬一起使用。
EN

Stack Overflow用户
提问于 2022-03-16 08:22:21
回答 1查看 1.1K关注 0票数 0

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

这是个老工程。它现在需要连接并运行剩余的旧包版本。该项目正处于运行阶段,现在需要与mongodb数据库连接。

代码示例如下所示:

package.json

代码语言:javascript
复制
 {
  "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

代码语言:javascript
复制
    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中连接。所以用户名和密码确实是正确的。

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

代码语言:javascript
复制
[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://,然后按如下方式返回Could not connect to MongoDB!

注意,mongoose版本是4.4.16

当我使用mongoose版本5或更高版本时,它将从node_modules/connect-mongo返回一个错误,如下所示:

代码语言:javascript
复制
...node_modules/connect-mongo/src/index.js:105
                    throw new Error('Connection strategy not found');
                    ^
    
    Error: Connection strategy not found
        at new MongoStore... 

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

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-27 12:15:30

我能够成功地连接,只从node 2.2.12 or later帐户选择MongoDB atlas选项。

字符串uri如下所示:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71493726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档