connecting to atlas mongo database

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-19 09:11:16

问题


I'm using node.Js, expressjs mongodb and Atlas

var Db     = require('mongodb').Db;
var Server = require('mongodb').Server;

the above method is not working for me. by using atlas database. you are given three nodes with three different host/Url now the problem here is that when I try to connect to mongodb.server it only ask for one host name (or its allowed to add many but I just don't know how)

my question would be, how can I make this work? like how can I join together 3 different Url and let 1 port let it in. and connect to database server


回答1:


you are given three nodes with three different host/Url now the problem here is that when I try to connect to mongodb.server it only ask for one host name

MongoDB Atlas provides you with a MongoDB Connection URI. The connection string should contain host(s) information.

You can also see a snippet example of MongoDB Node.js connecting to MongoDB Atlas on the manual MongoDB Atlas: Node.js Driver Example

MongoClientURI uri = new MongoClientURI(
   "mongodb+srv://user:password@cluster0.mongodb.net/");

MongoClient mongoClient = new MongoClient(uri);
MongoDatabase database = mongoClient.getDatabase("databaseName");

MongoDB Version 3.4 and earlier:

var MongoClient = require('mongodb').MongoClient;

var uri = "mongodb://user:password@mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin";
MongoClient.connect(uri, function(err, db) {
  db.close();
});

For other drivers, please see MongoDB Atlas: Connect via Driver



来源:https://stackoverflow.com/questions/42585295/connecting-to-atlas-mongo-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!