如何在所有字段上应用此Regex过滤器:
var collection = _DB.GetCollection<BsonDocument>(table);
FilterDefinition<BsonDocument> filter = Builders<BsonDocument>.Filter.Regex(__ANY FIELD__, BsonRegularExpression.Create(new Regex(".*" + searchString + ".*", RegexOptions.IgnoreCase)));
var documents = await collection.Find(filter).ToListAsync();
答案 0 :(得分:0)
显然,即使使用本机MongoDB语法(refer here)直接在控制台中也无法做到这一点
所以我最后提到所有字段并对过滤器进行ORing,如下所示:
var collection = _DB.GetCollection<BsonDocument>(table);
FilterDefinition<BsonDocument> filter;
foreach (Field field in allFields)
{
if (filter == null)
filter = Builders<BsonDocument>.Filter.Regex(field.Name, BsonRegularExpression.Create(new Regex(".*" + searchString + ".*", RegexOptions.IgnoreCase)));
else
filter = filter | Builders<BsonDocument>.Filter.Regex(col.Name, BsonRegularExpression.Create(new Regex(".*" + searchString + ".*", RegexOptions.IgnoreCase)));
}
var documents = await collection.Find(filter).ToListAsync();