How to do raw mongodb operations in mongoose?

Freewind Source

I'm asking this because when I write unit tests, I want to drop the test database and insert some initialize data, and also check the data in mongodb in testing. So I need raw operations to mongodb.

How to do this in mongoose? What I can do now is just create the connection, and not find any document in mongoose's official site.

 var mongoose = require('mongoose');

 // get the connection
 var conn = mongoose.connection;

But how to:

  1. drop the database
  2. create a collection
  3. write some data to a collection
  4. query a collection
  5. drop a collection


answered 6 years ago Jamund Ferguson #1

See the section on "Driver Access" in the docs:

Basically you can get access to the node-mongodb-native driver by doing YourModel.collection and then you can insert or remove or drop or whatever you need.

There's not a doc, but with this approach you'll get access to everything in here:


In your case you may want to skip using mongoose in your test suite and use the node-mongodb-native directly, or even write a simple mongodb shell script that can be run before your tests start.

answered 2 years ago steampowered #2

You can run native mongodb commands by using mongoose.connection.db. This accesses the native MongoDB driver, and you don't need to create a model.

An insert

  username: 'user1',
  firstName: 'Steve',
  lastName: 'LastName', 

An update

  {someFilterProperty: true},
  {$set: {
     siteId: new mongoose.mongo.ObjectId('56cb91bdc5946f14678934ba'),
     hasNewSiteId: true}},
  {multi: true});

You can send every command specific to that database using the database connection db reference mongoose.connection.db.

This is the mongoose API doc:

answered 2 years ago jitendra rajput #3

use this to run raw operations in mongoose.

  Model_name.collection.insertMany(array, { ordered: false },function(err, success){

answered 8 months ago 2oppin #4

Have encountered same trouble, to cleanup DBs after tests, and actual answer only confused because of absence "code blocks", so dig docs/code once more, for others-time-saving purpose posting this ;)

Mongoose collection extends Mongodb collection

/* * section collection.js * */

interface CollectionBase extends mongodb.Collection {

Documentation :

Same goes for the connection:

The Connection class exposed by require('mongoose') is actually the driver's NativeConnection class. connection.js defines a base class that the native versions extend. See:

So all "RAW" operations can be performed on collection/connection, assuming that you have

 var connection = mongoose.connection;


1.drop the database:


2.create a collection

connection.collection('newcollection') // creates if not exists

3.write some data to a collection

  { insertOne: { whatewer: { you: 'need' } } },

4.query a collection

that's obviously not a question: findAll, find, aggregate, all allowed (see the Docs)

5.drop a collection


comments powered by Disqus