I need to insert some data using mongoose but the name of the collection is provided by the user at the moment of the insertion, so I first have to check if the collection exists.
The way I know how to check if a collection exists is by querying the system.namespaces
collection. I can see 3 possible approaches to doing that.
- Find a way to query
system.namespaces
using mongoose (maybe defining a schema that matches the one in the db).
- Getting some underlying node-mongodb-native object from mongoose and performing the query manually. In any case, this is something I would like to learn how to do.
- Using a separate instance of a node-mongodb-native (or some other driver) to perform the query
Number 3
is the least elegant and the one i'm trying to avoid, I don't want to load another instance of the driver nor create a new connection when mongoose already created one.
I'm going to try number 1
after writing this. I just checked system.namespaces
and the schema looks quite simple
I'd still like to hear some opinions.
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…