woutr_be
woutr_be

Reputation: 9732

Mongoose plural collection definition

I'm trying to create different collections based on a specific id, the way that I do that is:

db.save({...}, 'collection_' + id).then(function() {
    deferred.resolve();
});

This works fine, I can see the new collection in my database.

However, when I'm trying to load this data, it doesn't work because mongoose forces it to be plurar.

var Items = mongoose.model('collection_' + id, MySchema);
Items.count(function(err, count) {
    console.log(count); = 0
});

The id is completely dynamic and varies on every request I make, how can I stop mongoose from adding an 's' to every collection name?

Upvotes: 1

Views: 262

Answers (1)

ZeMoon
ZeMoon

Reputation: 20284

You can explicitly set the collection name for a model as follows:

var collectionName = 'collection_' + id;
var Items = mongoose.model(collectionName, MySchema, collectionName);
//                         ^Change this if you want      ^This sets the collection name  

Check this answer for more ways to set the collection name.

Upvotes: 1

Related Questions