Emptying a database?

4,265 views
Skip to first unread message

David Todd

unread,
Apr 2, 2011, 11:02:56 AM4/2/11
to Mongoose Node.JS ORM
I'm using a particular database just for testing purposes, and I'd
like to empty it before running the test suite. Is there a built in
command to either empty or drop it?

Ben Nichols

unread,
Apr 10, 2011, 10:51:24 PM4/10/11
to Mongoose Node.JS ORM
I have the same question. Anyone?

Siegfried Ehret

unread,
Apr 11, 2011, 9:52:03 AM4/11/11
to mongoo...@googlegroups.com
Dropping collections is db.<collection_name>.drop();
db.drop() doesn't work ?

Ben Nichols

unread,
Apr 14, 2011, 1:55:09 PM4/14/11
to Mongoose Node.JS ORM
The way I got it to work is <collection_name>.collection.drop();

In your example where is the "db" coming from?

songhe yang

unread,
Apr 25, 2011, 3:28:47 AM4/25/11
to Mongoose Node.JS ORM
Drop the collection:

model.collection.drop();

e.g:

var mongoose = require('mongoose')
, Schema = mongoose.Schema
, Collection = mongoose.Collection;

mongoose.connect('mongodb://localhost/test');

var TestSchema = new Schema({
user_id : {type : Number, index : true}
,username : {type : String}
});

var model_name = coll_name = 'taobao';
mongoose.model(model_name, TestSchema, coll_name);

var TAOBAO = mongoose.model(model_name, coll_name);
var taobao = new TAOBAO();
taobao.collection.drop();

Hope this will help you.

Clint H.

unread,
Oct 7, 2011, 11:55:11 AM10/7/11
to mongoo...@googlegroups.com
When my unit tests finish (including on uncaughtException) I just drop the entire database like this:

mongoose.connection.db.executeDbCommand( {dropDatabase:1}, function(err, result) {
    console.log(err);
    console.log(result);
    process.exit(0);
});

Clint H.

unread,
Oct 7, 2011, 11:58:43 AM10/7/11
to mongoo...@googlegroups.com
Also, I found the following MongoDB docs page to be really helpful--it has several examples of how commands are "JSON formatted" for something like Mongoose's executeDbCommand() function, vs. the normal examples of running the commands in the Mongo console: http://www.mongodb.org/display/DOCS/List+of+Database+Commands
Reply all
Reply to author
Forward
0 new messages