0
> db.createCollection('P12345');
{ "ok" : 0, "errmsg" : "collection already exists", "code" : 48 }
> db.createCollection('P123456');
{ "ok" : 1 }
> db.createCollection('P123451');
{ "ok" : 1 }

I'm trying to do same create, a collection using mongo native driver & node.js

MongoClient.connect(url,{ useNewUrlParser: true },(err,client)=>{
if(err){
    console.log(err);
}
const db = client.db('Customers');
db.createCollection('P987654321').then((result)=>{
    console.log(result);
}).catch((err)=>{
    console.log(err);
});
client.close();
})

But I see the below error form running this code block.

{ MongoError: topology was destroyed
at executeCommand (F:\test\node_modules\mongodb\lib\operations\db_ops.js:473:21)
at db.listCollections.setReadPreference.toArray (F:\test\node_modules\mongodb\lib\operations\db_ops.js:232:7)
at result (F:\test\node_modules\mongodb\lib\utils.js:414:17)
at executeCallback (F:\test\node_modules\mongodb\lib\utils.js:406:9)
at handleCallback (F:\test\node_modules\mongodb\lib\utils.js:128:55)
at cursor.close (F:\test\node_modules\mongodb\lib\operations\cursor_ops.js:211:62)
at handleCallback (F:\test\node_modules\mongodb\lib\utils.js:128:55)
at completeClose (F:\test\node_modules\mongodb\lib\cursor.js:893:14)
at CommandCursor.Cursor.close (F:\test\node_modules\mongodb\lib\cursor.js:912:10)
at cursor._next (F:\test\node_modules\mongodb\lib\operations\cursor_ops.js:211:23) name: 'MongoError', [Symbol(mongoErrorContextSymbol)]: {} }
David
  • 15
  • 7
  • https://stackoverflow.com/questions/30909492/mongoerror-topology-was-destroyed – Aritra Chakraborty Nov 27 '18 at 20:53
  • OK I found removing client.close(); resolves the topology error but the output is something different a 2000+ lines of JSON is there a better way to find if collection is created because the output is same even if collection exists. – David Nov 27 '18 at 21:04
  • You could use listCollections to find out if a collection already exists: http://mongodb.github.io/node-mongodb-native/2.0/api/Db.html#listCollections – Bajal Nov 27 '18 at 22:46

0 Answers0