我想要实现efcode的expression功能,恰好mongodb.driver的Builders.Filter 支持&(and),|(or)
以下示例
- 安装依赖
MongoDB.Driver 2.27.0 2.27.0
- 定义model以及helper
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using System.Text.Json;
namespace MongoDBExpressionFilter.Models
{
public class Person
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("firstName")]
public string FirstName { get; set; }
[BsonElement("lastName")]
public string LastName { get; set; }
[BsonElement("age")]
public int Age { get; set; }
}
public class MongoDbContext
{
private readonly IMongoDatabase _database;
public MongoDbContext()
{
var client = new MongoClient("mongodb://ellis:ellischen@192.168.214.133:32000");
_database = client.GetDatabase("ellis");
}
public IMongoCollection<Person> Persons => _database.GetCollection<Person>("Persons");
}
public static class FilterBuilder
{
public static FilterDefinition<Person> BuildFilter(Dictionary<string, JsonElement> filters)
{
var builder = Builders<Person>.Filter;
var filter = builder.Empty; // Start with an empty filter
foreach (var item in filters)
{
var fieldName = item.Key;
var fieldValue = item.Value;
switch (fieldValue.ValueKind)
{
case JsonValueKind.String:
filter &= builder.Eq(fieldName, fieldValue.GetString());
break;
case JsonValueKind.Number:
if (fieldValue.TryGetInt32(out int intValue))
{
filter &= builder.Eq(fieldName, intValue);
}
else if (fieldValue.TryGetInt64(out long longValue))
{
filter &= builder.Eq(fieldName, longValue);
}
else if (fieldValue.TryGetDouble(out double doubleValue))
{
filter &= builder.Eq(fieldName, doubleValue);
}
break;
case JsonValueKind.True:
case JsonValueKind.False:
filter &= builder.Eq(fieldName, fieldValue.GetBoolean());
break;
// Add more cases as needed for different JsonValueKind types.
default:
throw new NotSupportedException($"Unsupported JSON value kind: {fieldValue.ValueKind}");
}
}
return filter;
}
}
}
- 依赖注入
builder.Services.AddScoped<MongoDbContext>();
- 使用
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;
using MongoDBExpressionFilter.Models;
using System.Text.Json;
namespace MongoDBExpressionFilter.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly MongoDbContext _context;
public ValuesController(MongoDbContext context)
{
_context = context;
}
[HttpPost]
public async Task<IActionResult> Filter([FromBody] Dictionary<string, JsonElement> filters)
{
var filter = FilterBuilder.BuildFilter(filters);
var persons = await _context.Persons.Find(filter).ToListAsync();
return Ok(persons);
}
}
}