Mongoose connections have a db property that lets you bypass Mongoose and talk to MongoDB directly. Here’s what you need to know.

Mongoose provides numerous powerful features, like middleware and validation. But sometimes you want to bypass Mongoose and use the MongoDB Node.js driver directly. Mongoose connections have a db property that let you access the MongoDB driver’s db handle:

// Connect to a MongoDB server running on 'localhost:27017' and use the
// 'test' database.
await mongoose.connect('mongodb://localhost:27017/test', {
  useNewUrlParser: true // Boilerplate for Mongoose 5.x
});

// Get the current db's profiling level using:
// http://mongodb.github.io/node-mongodb-native/3.6/api/Db.html#profilingLevel
// Mongoose doesn't support getting the profiling level.
const profilingLevel = await mongoose.connection.db.profilingLevel();
profilingLevel; // 'off'

#mongoose #mongodb #developer

The `db` Property in Mongoose
2.25 GEEK