How to get collection name from a Mongoose model o

2020-06-09 03:54发布

问题:

I have a mongoose model like so:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

let schema = new Schema({
    test: String
}, {
    collection: "test"
});

let model = mongoose.model("TestModel", schema);

How do I retrieve the collection name if in a callback I only have access to the "model" reference.

Expecting something like:

model.getCollectionName();

回答1:

Just use :

model.collection.collectionName

You also have a lot of usefull informations in model.collection such as collection options.