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.
system.namespaces
using mongoose (maybe defining a schema that matches the one in the db).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!
The collectionExists method can be used to check whether a collection is present or not: MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient. getDB("baeldung"); String testCollectionName = "student"; System.
What is exists() in Mongoose? exists is a Mongoose method or function that is used to check if at least one document exists that matches a specified filter. If there is a match, true is returned. Otherwise, false is returned.
If a collection does not exist, MongoDB creates the collection when you first store data for that collection. You can also explicitly create a collection with various options, such as setting the maximum size or the documentation validation rules.
Mongoose by default does not create any collection for the model in the database until any documents are created. The createCollection() method is used to create a collection explicitly.
Option 2 is probably the cleanest. Assuming you have a Mongoose Connection
object named conn
that's been opened using mongoose.createConnection
, you can access the native mongo Db
object via conn.db
. From there you can call collectionNames
which should provide what you're looking for:
conn.db.collectionNames(function (err, names) { // names contains an array of objects that contain the collection names });
You can also pass a collection name as a parameter to collectionNames
to filter the results to just what you're looking for.
Mongoose 4.x Update
In the 2.x version of the MongoDB native driver that Mongoose 4.x uses, collectionNames
has been replaced by listCollections
which accepts a filter and returns a cursor so you would do this as:
mongoose.connection.db.listCollections({name: 'mycollectionname'}) .next(function(err, collinfo) { if (collinfo) { // The collection exists } });
This works for me (mongoose version 5.1.1):
const mongoose = require('mongoose'); const mongoURI = 'mongodb://localhost:27017/mydb' // notice the mongoose.createConnection instead of mongoose.connect const conn = mongoose.createConnection(mongoURI); conn.on('open', function () { conn.db.listCollections().toArray(function (err, collectionNames) { if (err) { console.log(err); return; } console.log(collectionNames); conn.close(); }); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With