Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do raw mongodb operations in mongoose?

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');  mongoose.connect('mongo://localhost/shuzu_test');   // 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
like image 668
Freewind Avatar asked May 09 '12 15:05

Freewind


People also ask

Is Mongoose good for MongoDB?

Mongoose, a neat ODM library for MongoDB used in Node. js projects, has plenty of useful features that make developers' lives easier. It manages relationships between data, has schema validation, and overall allows coding 3-5 times faster.

Can I use MongoDB and Mongoose at the same time?

yes you should, its a good practice. Mongoose requires a connection to a MongoDB database. You can use require() and connect to a locally hosted database with mongoose.

Which is better MongoDB or Mongoose?

Mongoose allows users to conveniently create and manage data in MongoDB. While it is possible to manage data, define schemas, etc. using MongoDB APIs, it is quite difficult to do so. Hence, Mongoose has made lives easier.


2 Answers

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

An insert

mongoose.connection.db.collection('userCollection').insert({   username: 'captain1',   firstName: 'Steve',   lastName: 'Rogers',  }); 

An update

mongoose.connection.db.collection('userCollection').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: http://mongoosejs.com/docs/api.html#connection_Connection-db

Important: Note some of the options in the NodeJS driver are different than the options in MongoDB shell commands. For example findOneAndUpdate() uses returnOriginal instead of returnNewDocument. See here and here for more on this.

like image 155
steampowered Avatar answered Nov 08 '22 10:11

steampowered


See the section on "Driver Access" in the docs: http://mongoosejs.com/

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: https://mongoosejs.com/docs/api.html#collection-js

Edit:

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.

like image 32
Jamund Ferguson Avatar answered Nov 08 '22 10:11

Jamund Ferguson