连接到Atlas Mongo数据库 [英] connecting to atlas mongo database
问题描述
我正在使用node.Js,expressjs mongodb和Atlas
I'm using node.Js, expressjs mongodb and Atlas
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
以上方法对我不起作用.通过使用地图集数据库.现在为您提供了三个带有三个不同主机/URL的节点,这里的问题是当我尝试连接到 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)
我的问题是,我该如何进行这项工作?就像我如何将3个不同的网址合并在一起,并让1个端口进入.并连接到数据库服务器
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
推荐答案
现在为您提供了三个具有三个不同主机/URL的节点,这里的问题是,当我尝试连接到mongodb.server时,它仅要求一个主机名
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地图集为您提供了
MongoDB Atlas provides you with a MongoDB Connection URI. The connection string should contain host(s) information.
您还可以在手册
MongoDB 3.4版及更早版本: MongoDB Version 3.4 and earlier: var MongoClient = require('mongodb').MongoClient; var MongoClient = require('mongodb').MongoClient; 有关其他驱动程序,请参见 MongoDB Atlas:通过驱动程序连接 For other drivers, please see MongoDB Atlas: Connect via Driver 这篇关于连接到Atlas Mongo数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!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();
});