Answer the question
In order to leave comments, you need to log in
Direct access to the collection from mongoose. mongodb + mongoose + node.js
Welcome all.
I am writing an application in node.js. The database is mongodb. I use mongoose to work with it.
There is one Schema, according to which many models are created. The structure and use of each model is the same, only the name is different. At the same time, new models are created as new users register. That is, a user with id = 1 registered -> created collection_1, id = 2 -> collection_2, etc.
Question: how, having only id, to get access to the required collection? In mongodb, everything is just db.collectionName, but I don’t know how to do it in mongoose.
Answer the question
In order to leave comments, you need to log in
I think something like this :)
function getModel(id, db) {
var colName = 'collection_' + id;
if (db.models[colName]) return db.models[colName];
else {
return db.models(colName, UserSchema, colName);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question