VJGOP
0
Q:

node js connect to mongodb using mongoose

// getting-started.js
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/name", { useUnifiedTopology: true, useNewUrlParser: true });
2
async function main(){
    /**
     * Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
     * See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
     */
    const uri = "mongodb+srv://<username>:<password>@<your-cluster-url>/test?retryWrites=true&w=majority";
 

    const client = new MongoClient(uri);
 
    try {
        // Connect to the MongoDB cluster
        await client.connect();
 
        // Make the appropriate DB calls
        await  listDatabases(client);
 
    } catch (e) {
        console.error(e);
    } finally {
        await client.close();
    }
}

main().catch(console.error);
1

//connect with mongodb
mongoose.connect('mongodb://localhost:27017/your_db_name', {useNewUrlParser: true});
//you can also specify with user and pass
mongoose.connect('mongodb://username:password@host:port/database?options...', {useNewUrlParser: true});
//or goto docs https://mongoosejs.com/docs/connections.html
0
mongoose.connect('mongodb://localhost:27017/myapp', {useNewUrlParser: true});
-2

New to Communities?

Join the community