Piruthuvi
Piruthuvi

Reputation: 49

mongoClient.connect VS mongoose.connect in MongoDB

I'm a beginner in backend.I have a doubt about MongoDB. What are the differences between mongoClient.connect and mongoose.connect in express framework?

Upvotes: 2

Views: 3330

Answers (1)

whoami - fakeFaceTrueSoul
whoami - fakeFaceTrueSoul

Reputation: 17935

These two can be different :

Usually when you use mongoClient.connect, then you should probably be using node-mongodb-native-driver

Sample Code :

const MongoClient = require('mongodb').MongoClient;
MongoClient.connect(url)

But when you see mongoose.connect then mongoose is being used in that project :

Sample Code :

const mongoose = require('mongoose')
mongoose.connect(url)

These two are different packages through which you can connect to MongoDB using node.js. If you use mongoose then probably you might not need nodejs-mongodb native driver as mongoose has a lot of functions which are basically wrappers to existing native driver. Internally mongoose would use native driver. If you wanted to make your MongoDB look schema based then mongoose can help you a lot as you'll define schema for your collection & operate on those schemas while data insertion.

Upvotes: 5

Related Questions