Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

check if the collection exists in mongodb database mongoose

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();
    });
});
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #check #collection #exists #mongodb #database #mongoose
ADD COMMENT
Topic
Name
3+9 =