连接到mongodb

我是MEAN开发中的新手,正在开发一个简单的应用程序,并且第一步是尝试连接到mongodb,因此我安装了node,express,morgan,mongodb,mongoose。>

这是我在index.js中的代码:

const express = require('express');
const morgan = require('morgan');
const app = express();

const { MongoClient } = require('./database');

// Settings
app.set('port',process.env.PORT || 3000);


// Middlewares
app.use(morgan('dev'));
app.use(express.json());

// Routes


// Starting the server
app.listen(app.get('port'),() => {
    console.log('server on port',app.get('port'));
});

,然后是我的database.js上的代码:

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://duke:<password>@cluster0-dreyi.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri,{ useNewUrlParser: true });
client.connect(err => {
  const collection = client.db("test").collection("devices");
  console.log("horrorrrrrr");
  // perform actions on the collection object
  client.close();
}); 

module.exports = MongoClient;

我还尝试了mongodb页面上的以下代码以连接到应用程序:

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://duke:<password>@cluster0-dreyi.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri,{ useNewUrlParser: true });
client.connect(err => {
  const collection = client.db("test").collection("devices");
  // perform actions on the collection object
  client.close();
});

我当然将密码更改为真实密码。今天请记住,这是我第一次接触mongodb以及MEAN完整堆栈,而我在连接中花费了太多时间。

这是我得到的错误:

(节点:5284)DeprecationWarning:不建议使用当前的“服务器发现和监视”引擎,并将在以后的版本中将其删除。要使用新的“服务器发现和监视”引擎,请将选项{useUnifiedTopology:true}传递给MongoClient构造函数。

编辑

@iLiA感谢您的回复!我尝试了您的代码,但没有用,我将向您展示如何使用真实密码进行操作:

const url = 'mongodb+srv://duke:password@cluster0-dreyi.mongodb.net/test?retryWrites=true&w=majority';

const mongoose = require('mongoose');
mongoose.connect(url,{
   useNewUrlParser: true,useCreateIndex: true,useUnifiedTopology: true,useFindAndmodify: false
})
.then(()=>{
    console.log('congrats,problem solved')
})
.catch((err)=>{
    console.log(`there is not a problem with ${err.message}`);
    process.exit(-1)
})



module.exports = mongoose;

,错误是: 服务器选择在30000毫秒后超时没有问题 [nodemon]应用程序崩溃-等待文件更改,然后再开始...

亲切的问候,

zhuo67760212 回答:连接到mongodb

我为为什么下载了<template> <div class="add-wave"> <h3>Add Wave</h3> <div class="row"> <form @click.prevent="addwave()" class="col s12"> <div class="row"> <div class="input-field col s12"> <input type="text" v-model="host" /> <label class="active">Host</label> </div> </div> <div class="row"> <div class="input-field col s12"> <input type="text" v-model="scout" /> <label class="active">Scout</label> </div> </div> <button type="submit" class="btn">Submit</button> <router-link to="/member" class="btn grey">Cancel</router-link> </form> </div> </div> </template> <script> import db from "../data/firebaseInit"; export default { data() { return { host: null,scout: null }; },methods: { addwave() { db.collection("members") .where("name","==",this.$route.params.host) .get() .then(querySnapshot => { querySnapshot.forEach(doc => { doc.ref .update({ host: db.FieldValue.increment(1) }) .then(() => { this.$router.push({ name: "member",params: { name: this.name } }); }); }); }); } } }; </script> mongodb而感到困惑,但这是猫鼬解决方案

mongoose

编辑:   看来您忘了在mongo地图集中将您的IP地址列入白名单。

本文链接:https://www.f2er.com/3150274.html

大家都在问