【问题标题】:How to connect to mongoDB compass with Node.js如何使用 Node.js 连接到 mongoDB 指南针
【发布时间】:2019-01-15 17:50:23
【问题描述】:

我正在尝试将数据从 node.js 发送到 mongoDB 罗盘服务器。我创建了一个 MongoDB 集群并下载了 Compass。我可以将 Compass 连接到集群,一切正常。

但是,当我尝试将 Node.js 服务器连接到 Compass 时出现错误,以下是我的节点代码。

const express = require('express');

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');



const app = express();

// Connect to mongodb
// Connection URL

const url = "mongodb://tfi-mfgbt.mongodb.net/test" ;

// Database Name
const dbName = 'TFI';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  const db = client.db(dbName);

  client.close();
});


const port = 5000;

app.listen(port, () => {
    console.log(`Server started on port ${port}`);
});

我在终端中运行 Node app.js 得到一个

MongoClient.connect.                                                                                                                                                              
Server started on port 5000                                                                                                                                                       
F:\code\vidjot\node_modules\mongodb\lib\operations\mongo_client_ops.js:439                                                                                                        
      throw err;                                                                                                                                                                  
      ^                                                                                                                                                                           

AssertionError [ERR_ASSERTION]: null == 'MongoNetworkError: failed to connect to server [tfi-mfgbt.mongodb.net:27017] on first connect [MongoNetworkError: getaddrinfo E          
    at F:\code\vidjot\app.js:20:10                                                                                                                                                
    at err (F:\code\vidjot\node_modules\mongodb\lib\utils.js:415:14)                                                                                                              
    at executeCallback (F:\code\vidjot\node_modules\mongodb\lib\utils.js:404:25)                                                                                                  
    at err (F:\code\vidjot\node_modules\mongodb\lib\operations\mongo_client_ops.js:284:21)                                                                                        
    at connectCallback (F:\code\vidjot\node_modules\mongodb\lib\operations\mongo_client_ops.js:240:5)                                                                             
    at process.nextTick (F:\code\vidjot\node_modules\mongodb\lib\operations\mongo_client_ops.js:436:7)                                                                            
    at _combinedTickCallback (internal/process/next_tick.js:131:7)                                                                                                                
    at process._tickCallback (internal/process/next_tick.js:180:9)                                                                                                                
[nodemon] app crashed - waiting for file changes before starting...                       

主机名 “mongodb://tfi-mfgbt.mongodb.net/test” 是我的 Compass 会话的主机名。如所见here

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:

    要连接 Mongo,我使用以下代码行:

        var mongoUrl = '"mongodb://tfi-mfgbt.mongodb.net/test"'
        var mongoose = require('mongoose')
        // updated 2021
        mongoose.Promise = global.Promise;
        mongoose.set('useNewUrlParser', true);
        mongoose.set('useFindAndModify', false);
        mongoose.set('useCreateIndex', true)
        
        mongoose.connect(mongoUrl, { useUnifiedTopology: true })
        .then(() => { log('Connected to MongoDB: %s \n ', mongoUrl) }) 
        .catch((err) => { error('MongoDB connection error: %s \n', err); })
    
        // Old connection
        //mongoose.connect(mongoUrl, { useMongoClient: true })
        //mongoose.connection.on('error', err => debug('MongoDB connection error: ${err}'));
    

    这应该适合你!

    查询:

    mySchema.find({},function(err, docs){... My code ...})
    

    【讨论】:

    • 感谢这工作!我赞成你,但我的水平太低,无法记录。但再次感谢!
    • 如果您在 2021 年尝试此操作,useMongoClient 将出错。只需删除 useMongoClient 并使用 .on('connected') 测试您的连接
    • Pots 更新到 2021 年的方式......谢谢@emre-ozgun
    猜你喜欢
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-19
    • 1970-01-01
    相关资源
    最近更新 更多