[英]How do I find all collections with a certain string in the name in my MongoDB database?
I've got a database containing several collections. 我有一个包含多个集合的数据库。 Some has got a name like "carpenter_title_year_version". 有些人的名字像“ carpenter_title_year_version”。 Others has got a name like "plumber_title_year_version". 其他人则有类似“ plumber_title_year_version”的名称。 How do I set up a filter to retrieve all collections where the string "carpenter" is in the collectionname? 如何设置过滤器以检索字符串“ carpenter”在集合名称中的所有集合?
I'm thinking something like: 我在想类似的东西:
var filterBuilder = Builders<GroupEntity>.Filter;
var projectionBuilder = Builders<GroupEntity>.Projection;
var collection = Database.GetCollection<GroupEntity>("dbname");
var filter = filterBuilder.ElemMatch("carpenter..., ?"); //<--- ???
var projection = projectionBuilder.Exclude("_id");
var list = await collection.Find(filter).Project(projection).ToListAsync();
There's (and an async version of it) 有(及其异步版本)
IAsyncCursor<MongoDB.Bson.BsonDocument> IMongoDatabase.ListCollections(ListCollectionsOptions options = null, CancellationToken cancellationToken = null);
You can use the filter from the collection options to match the collection name. 您可以使用集合选项中的过滤器来匹配集合名称。
// <CAPS INSIDE> are wildcards
var _client = new MongoClient(@"connection string");
var _database = _client.GetDatabase("<DATABASE NAME>");
var bre = new BsonRegularExpression("<YOUR REGEX PATTERN>");
var copt = new ListCollectionsOptions{ Filter =
Builders<BsonDocument>.Filter.Regex ("name", bre )
};
var collectionsWithMatchingNames = _database.ListCollections(copt).ToList().Select (col => col["name"]);
Only then you get your particular collection, like: 只有这样,您才能得到自己的特定收藏,例如:
foreach (var x in collectionsWithMatchingNames)
var collection = _database.GetCollection<BsonDocument>(x);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.