我将MongoClient与micronaut MongoDb一起使用。尝试执行查询运算符以在mongoDb中创建查询。
public static Bson QueryBuilder(ProductSearchCriteria productSearchCriteria){
Bson query = null;
if (productSearchCriteria.getName() != null){
query = Filters.eq("name", productSearchCriteria.getName());
}
if(productSearchCriteria.getDescription() != null){
query = Filters.eq("description", productSearchCriteria.getDescription());
}
return query;
}
现在如何在一个查询中执行逻辑AND / OR / NOT / NOR和其他运算符
答案 0 :(得分:0)
我这样做了,不知道这是不是正确的方法
public static List<Bson> QueryBuilder(ProductSearchCriteria productSearchCriteria){
List<Bson> filters = new ArrayList<>();
final BeanIntrospection<Product> introspection = BeanIntrospection.getIntrospection(Product.class);
var product = introspection.getPropertyNames();
if (productSearchCriteria.getName() != null){
filters.add(Filters.eq(product[0], productSearchCriteria.getName()));
}
if(productSearchCriteria.getDescription() != null){
filters.add(Filters.eq(product[2], productSearchCriteria.getDescription()));
}
return filters;
}