mongodb本地驱动程序获取没有数据库名称的集合名称

我怎样才能得到没有数据库名称的mongodb本地驱动程序为nodeJS集合名称?

db.collectionNames(function(err, collections) { if (err) { log.error(err); } else { log.info(collections); } }); 

此代码返回如下所示:

databaseName.collection1,databaseName.collection2,databaseName.collection3

但我想得到的名字: collection1, collection2, collection3

响应的确切结构是在数组中具有“名称”键的子文档:

 [ { name: 'test.cursors' }, { name: 'test.episodes' }, { name: 'test.zips' }, { name: 'test.scripts' } ] 

所以只需使用正则expression式replace map

 db.collectionNames(function(err, collections) { console.log( collections.map(function(x) { return x.name.replace(/^([^.]*)./,""); }) ); }); 

这将剥夺一切到第一. 这是数据库前缀。 以防万一你真的有一个集合名称. 在他们中。

使用MongoDB 2.0.0及更高版本的驱动程序,您需要使用listCollections() ,如下所示

 db.listCollections().toArray(function(err, collections){ //collections = [{"name": "coll1"}, {"name": "coll2"}] });