如何通过filter获取所有集合名称?
最近我想用mongodb驱动程序通过filter来获取所有的集合名称。
"Admin", "Config", "Event", "event12345678901234567890123456789012", "system.indexes"
以上是所有集合的名称,但我只想显示集合,以“事件”开始,我发现文档有一个过滤参数,但文档不显示任何用法和过滤规则,所以有没有人知道如何用它?
由于getCollectionNames()
基于查询db.system.namespaces
:
<database> .system.namespaces集合包含有关所有数据库集合的信息。 其他名称空间元数据存在于database.ns文件中,对数据库用户不透明。
> db.getCollectionNames; function (){ var all = []; var nsLength = this._name.length + 1; var c = this.getCollection( "system.namespaces" ).find(); while ( c.hasNext() ){ var name = c.next().name; if ( name.indexOf( "$" ) >= 0 && name.indexOf( ".oplog.$" ) < 0 ) continue; all.push( name.substring( nsLength ) ); } return all.sort(); }
您可以使用$regex
手动过滤db.system.namespaces
:
db.system.namespaces.find({'name': {"$regex": /^foo.Event\w*$/, "$options": "-i"}});
其中foo
是数据库名称, -i
有助于进行不区分大小写的search。
演示(来自“mongo shell” ):
> db.system.namespaces.find(); { "name" : "foo.system.indexes" } { "name" : "foo.system.users.$_id_" } { "name" : "foo.system.users.$user_1_userSource_1" } { "name" : "foo.system.users" } { "name" : "foo.foo.$_id_" } { "name" : "foo.foo" } { "name" : "foo.test.$_id_" } { "name" : "foo.test", "options" : { "create" : "test" } } { "name" : "foo.Config.$_id_" } { "name" : "foo.Config", "options" : { "create" : "Config" } } { "name" : "foo.Event.$_id_" } { "name" : "foo.Event", "options" : { "create" : "Event" } } { "name" : "foo.event12345678901234567890123456789012.$_id_" } { "name" : "foo.event12345678901234567890123456789012", "options" : { "create" : "event12345678901234567890123456789012" } } { "name" : "foo.Admin.$_id_" } { "name" : "foo.Admin", "options" : { "create" : "Admin" } } > db.system.namespaces.find({'name': {"$regex": /^foo.Event\w*$/, "$options": "-i"}}); { "name" : "foo.Event", "options" : { "create" : "Event" } } { "name" : "foo.event12345678901234567890123456789012", "options" : { "create" : "event12345678901234567890123456789012" } }
- 有没有办法使用node-mongodb-native的dynamic密钥?
- 为什么我似乎不能将普通对象合并到Mongo文档中?
- 为什么Meteor.users.findOne({username:“test”})返回一个对象,但用具有相同值的variablesreplace“test”会返回undefined
- 是否有可能在MongoDB中“join”文档并删除“父”文档,但仍保留“子”文档?
- 部署Meteor和MongoDB到Heroku
- 用mongoose模式生成假数据
- 如何用MongoDB构buildrecursion结构
- Mongodb:数据版本与search
- 无法从MongoDB数据库获取数据的Web应用程序