mongodb native driver get collection names without database name
javascript, mongodb, node-mongodb-native, node.js
Solution
The exact structure of the response is a sub-document with the "name" key in an array:
[
{ name: 'test.cursors' },
{ name: 'test.episodes' },
{ name: 'test.zips' },
{ name: 'test.scripts' }
]
So just use `map` with a regex `replace`:
db.collectionNames(function(err, collections) {
console.log(
collections.map(function(x) {
return x.name.replace(/^([^.]*)./,"");
})
);
});
And that will strip out everything up to the first `.` which is the database prefix. Just in case you actually have collection names with a `.` in them.
Problem
How can I get collection names without database name from mongodb native driver for nodeJS? ``` db.collectionNames(function(err, collections) { if (err) { log.error(err); } else { log.info(collections); } }); ``` This code returns something like this: databaseName.collection1, databaseName.collection2, databaseName.collection3 But i want to get names: `collection1, collection2, collection3`