Mongodb mongo db获取集合从

Mongodb mongo db获取集合从,mongodb,Mongodb,我有很多收藏的名字,比如 app_users5473275725743 app_users5473275725746 app_users5473275725747 app_users5473275725748 我希望能够找到以名称“app_users”开头的所有集合,而db.getCollectionNames返回所有集合名称。您可以使用过滤器功能仅提取相关集合名称: db.getCollectionNames().forEach(function(collName) { if (co

我有很多收藏的名字,比如

app_users5473275725743
app_users5473275725746
app_users5473275725747
app_users5473275725748

我希望能够找到以名称“app_users”开头的所有集合,而db.getCollectionNames返回所有集合名称。

您可以使用
过滤器
功能仅提取相关集合名称:

db.getCollectionNames().forEach(function(collName) {
    if (collName.startsWith("app_users")) {
        print(collName);
    }
});
var cols = db.getCollectionNames().filter( function( col ) { 
  return col.startsWith( "app_users" ) 
} )
此代码将使用以
app\u users
开头的集合名称数组填充
cols
变量

参考: