January 10, 2023

How to configure mongodb on node js with example

 

Try the below steps to configure mongodb on node js :

1. Install mongoose

npm install mongoose --save 2. Create a new Mongoose model var Model = require ( ' mongoose ' );
var User = new Model ();
User . name = ' John ' ;
User . age = 25 ;
3. Connect to the MongoDB server var mongoose = require ( ' mongoose ' );
var db = mongoose . connect ( ' localhost:27017/users ' );

// or use a connection string
var db = new mongoose.Db ( ' users ' , { host : ' localhost ' });

// or use a connection pool
var pool = new mongoose.DbPool ( 10 );

// or use a connection URI
var uri = "mongodb://localhost:27017/users " ;
db . connect (uri);

// or use a named instance
var instance = db . createConnection ( " mydb " );
instance . connect ();

// or use an existing database instance
var db = new mongoose.Db ( " users " , { host : " localhost " });

// or use a connection pool
var pool = new mongoose.DbPool ( 10 );

// or use a connection URI
var uri = "mongodb://localhost:27017/users " ;
db . connect (uri);
4. Create a model instance and query it var user = db . createModel ({ name : " John " , age : 25 });
user . save ();
console . log (user);
// {name: "John", age: 25}

Hope the above helps

Thanks,
Author

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Comment and Reviews

Please Note For Leaving Comments, Reviews and Queries on the Blog Post You would have to Join the Site. This is needed to prevent Spam.

Blog Join Link is Provided Below.

Thanks,
Author

Followers

Popular Posts