javascript - mongodb native driver get collection names without database name -
how can collection names without database name mongodb native driver nodejs?
db.collectionnames(function(err, collections) { if (err) { log.error(err); } else { log.info(collections); } });
this code returns this:
databasename.collection1, databasename.collection2, databasename.collection3
but want names: collection1, collection2, collection3
the exact structure of response sub-document "name" key in array:
[ { name: 'test.cursors' }, { name: 'test.episodes' }, { name: 'test.zips' }, { name: 'test.scripts' } ]
so use map
regex replace
:
db.collectionnames(function(err, collections) { console.log( collections.map(function(x) { return x.name.replace(/^([^.]*)./,""); }) ); });
and strip out first .
database prefix. in case have collection names .
in them.
Comments
Post a Comment