0

Im currently designing a test envi where i need to reset all connected mongodb databases when the testings are all done ( across all test cases.. ).

Code Here

Now i tried the following,

Mongoose.js: remove collection or DB

drop database with mongoose

I still cant make it work.

So what will I do aside from giving up?

Community
  • 1
  • 1
  • did you tried connections.db1.dropDatabase(); – Prasanth Jaya Apr 12 '17 at 05:10
  • yeah i tried that one also, but still the second database **db2** is completely dropped. – icyber-ghost Apr 12 '17 at 06:39
  • i guess your problem lies due to multiple connection. When you connect to the first db1, db1 is the live connection and when you connect to db2 then you live connection switched to db2 not db1. so it is only deleting the db2. – Prasanth Jaya Apr 12 '17 at 06:49
  • To confirm the above issue, connect both dbs like you did so fat, try to delete db2 first and try connecting to db1 again and delete db1 now. – Prasanth Jaya Apr 12 '17 at 06:50
  • you mean this, ` await connections.db2.dropDatabase(); await connections.d1.dropDatabase()`? by this still the **db1** is not completely dropped. but i notice something **users collection** retain its structure but no records at all. – icyber-ghost Apr 12 '17 at 07:13

2 Answers2

0

added the assurance that drop the databases only if successfully connected.

My Answer

Community
  • 1
  • 1
0

As an alternative approach, Mongoose does export a constructor for a new instance on the default instance. So something like this is possible.

var Mongoose = require('mongoose').Mongoose;

var instance1 = new Mongoose();
instance1.connect('foo');`

var instance2 = new Mongoose();
instance2.connect('bar');

This is very useful when working with separate data sources, and also when you want to have a separate database context for each user or request. You will need to be careful, as it is possible to create a LOT of connections when doing this. Make sure to call disconnect() when instances are not needed, and also to limit the pool size created by each instance.

Prasanth Jaya
  • 4,407
  • 2
  • 23
  • 33