gpt4 book ai didi

c# - 使用和或运算符的 Mongodb c# 过滤器

转载 作者:可可西里 更新时间:2023-11-01 10:48:31 26 4
gpt4 key购买 nike

我在 mongodb 中有 json 文档

示例

{
"SchemaName": "Intelligence",
"SchemaDescription": "WindPower",
"SchemaType": "WindPower",
"SchemaTypeId": 1,
"SchemaData": {
"ProjectId": 1,
"LastUpdated": "2016-07-02T19:27:28.000+0000",
"ProjectName": "Zhonghuashan II",
"Capacity": 49.0,
"Technology": "Onshore",
"Country":{
"CountryId":1,
"CountryName":"UnitedKingdom",
"CountryCode":"UK"
}
}
}

现在我根据搜索条件过滤数据

var filter = Builders<Schema>.Filter.Or(
Builders<Schema>.Filter.Where(p => p.SchemaData.ProjectName.ToLower().Contains(searchCriteria.ProjectName.ToLower())),
Builders<Schema>.Filter.Where(p => p.SchemaData.Technology.ToLower().Contains(searchCriteria.Technology.ToLower())),
Builders<Schema>.Filter.Where(p => p.SchemaData.Country.CountryName.ToLower().Contains(searchCriteria.Country.ToLower()))
);
var list = await collectionHandler.ReadOnly<Schema>().FindSync(filter).ToListAsync();
return list;

我需要添加条件

  1. searchCriteria.ProjectName =""|| searchCriteria.Technology=""|| searchCriteria.Country = ""= 返回所有记录

  2. searchCriteria.ProjectName ="abc"和 searchCriteria.Technology="xyz"|| searchCriteria.Country = ""= 返回匹配的记录

  3. searchCriteria.ProjectName ="abc"and searchCriteria.Technology="xyz"and searchCriteria.Country = "pqr"= 返回匹配的记录

  4. searchCriteria.ProjectName =""|| searchCriteria.Technology="xyz"and searchCriteria.Country = "pqr"= 返回匹配的记录

  5. searchCriteria.Technology=""="abc"|| searchCriteria.Technology="xyz"and searchCriteria.Country = "pqr"= 返回匹配的记录

假设搜索条件的任何属性都可以与 and 和 or 与搜索条件的其他属性组合

最佳答案

对我来说,我很乐意直接将过滤器作为字符串传递:

    FilterDefinition<BsonDocument> filter = @"{ ""Prop"": { $Or: [{ $and: [...] },{ $and: [...] }] } }";
var lst = collection.Find(filter);

您可以查看文档:
https://docs.mongodb.com/v3.2/reference/operator/query/or/

关于c# - 使用和或运算符的 Mongodb c# 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47036451/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com