Create a mongodb database on mongodb atlas
Create a free account of mongodb
1, Go to the official site https://www.mongodb.com/, click start free button.
2, Fill in account profile and hit cotinue to submit sign up info.
3, After create successfully, then login with your new account.
Create a new Cluster(database)
1, Build a new cluster first.
2, Get the connect string of the database.
- click connect
- add IP whitelist You can add “0.0.0.0/0” by default. and then you can access it from anywhere.
- copy the URI Connection String Replace
with the db user you created, and with the name of the database to which you wish to connect.
Node.js Driver Example
var MongoClient = require('mongodb').MongoClient;
var uri = "mongodb://kay:myRealPassword@mycluster0-shard-00-00-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin";
MongoClient.connect(uri, function(err, db) {
db.close();
});
To connect to a database other than admin but still authenticate to the admin database, update the database component of the connection string.
mongodb://username:password@host1:port1,...,hostN:portN/database?authSource=admin&...
For example, the following connection string specifies test in the database component and includes the authSource=admin option.
// `kay` is the username
// `myRealPassword` is the password
var uriTestDb = "mongodb://kay:myRealPassword@mycluster0-shard-00-00-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/test?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin";
MongoClient.connect(uriTestDb, function(err, db) {
db.close();
});