Sandeep 905
Sandeep 905

Reputation: 37

Should I use both MongoDB and Mongoose in Node.js?

I am new to MongoDB and I use MongoDB locally but in some cases I need to use Mongoose. How to use both MongoDB and Mongoose in the same project. Please help me to resolve this issue and please put if you have any references.

Upvotes: 2

Views: 2257

Answers (3)

Rizwan
Rizwan

Reputation: 441

Mongoose is a full package, it has all the methods required by a backend. It has a connections function, it has ORM which helps to do CRUD operations. It is like an industry standard.

Upvotes: 0

Gyan Prakash
Gyan Prakash

Reputation: 17

yes you should, its a good practice.

npm install mongoose

Mongoose requires a connection to a MongoDB database. You can use require() and connect to a locally hosted database with mongoose.connect().

//Import the mongoose module
var mongoose = require('mongoose');

//Set up default mongoose connection
var mongoDB = 'mongodb://127.0.0.1/my_database';
mongoose.connect(mongoDB, {useNewUrlParser: true, useUnifiedTopology: true});

//Get the default connection
var db = mongoose.connection;

//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

If you need to create additional connections you can use mongoose.createConnection(). This takes the same form of database URI (with host, database, port, options etc.) as connect() and returns a Connection object)

Upvotes: 1

Arayik Hovhannisyan
Arayik Hovhannisyan

Reputation: 97

MongoDB is a database, while Mongoose is the "bridge" between MongoDB and your server. You use it to create schemas and connect to MongoDB. Please see this for more in depth answer for your question.

Upvotes: 3

Related Questions