当列表字段中的值与列表中的条件值匹配时,在 C# 中使用 LINQ 查找 MondoDB 记录 [英] Use LINQ in C# to find MondoDB records when values in a list field match a criteria value from a list

查看:74
本文介绍了当列表字段中的值与列表中的条件值匹配时,在 C# 中使用 LINQ 查找 MondoDB 记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 LINQ 返回 MongoDB 集合中的所有记录,其中记录中的字段是字符串列表,并且列表中的任何字符串与用作搜索条件的字符串列表中的任何字符串值匹配:

集合中的 Mongo 记录(项目"):

<代码>{_ID": ...,字符串列表":[字符串1",字符串2",字符串3"],...}

搜索条件:

var criteria = new List{ "string2", "string4" };

我的代码:

var foundItems = iMongoDataProvider.FindAll().Where(x =>x.StringList.ContainsAny(criteria)).ToList();

基于上述内容,应返回 Mongo 记录,因为 StringList 值之一与搜索标准中的值之一匹配.即使我可以手动仔细阅读集合并找到匹配的记录,也不会返回任何内容.我究竟做错了什么?有人可以提供一个例子来满足我的需求吗?谢谢!

解决方案

您正在寻找的是 ElemMatch 过滤器 (https://docs.mongodb.com/v3.2/reference/operator/query/elemMatch/) :

var foundItems = collection.Find(Builders.Filter.ElemMatch(x =>x.StringList,s=>criteria.Contains(s)));

其中集合是您的 IMongoCollection我知道,您正在使用 FindAll,这意味着您的 MongoDb 驱动程序是 1.x 版(请参阅此处了解更多信息:MongoDB .NET 驱动程序 2.0 中的 FindAll)我建议更新您的驱动程序,因为此版本不是最新的.或者有什么重要的原因不这样做?

服务器上的此过滤器查询.当然,您可以将数据设为 IEnumerable 并在本地进行过滤:

var foundItems = collection.Find(x=>true).ToEnumerable().Where(x => x.StringList.Intersect(criteria).Any());

如果您的数据不是很大,并且您可以在客户端进行过滤,那么这也是一个不错的方法.

如果你已经在做FindAll,这意味着你得到了所有的数据,你可以用intersect查询它:

var foundItems = iMongoDataProvider.FindAll().Where(x => x.StringList.Intersect(criteria).Any());

I want to use LINQ to return all records in a MongoDB collection where the field in the record is a list of strings and any string in the list matches any string value in a list of strings used as the search criteria:

Mongo Record in Collection ("Item"):

{
    "_id": ...,
    "StringList": [
        "string1",
        "string2",
        "string3"
    ],
    ...
}

Search Criteria:

var criteria = new List<string> { "string2", "string4" };

My Code:

var foundItems = iMongoDataProvider.FindAll<Item>()
                           .Where(x =>x.StringList.ContainsAny(criteria)).ToList();

Based on the above, the Mongo record should be returned since one of the StringList values matches one of the values in the search criteria. Nothing is returned even though I can manually peruse the collection and find the matching record. What am I doing wrong? Can someone provide an example that will do what I need? Thanks!

解决方案

What you are looking for is ElemMatch Filter (https://docs.mongodb.com/v3.2/reference/operator/query/elemMatch/) :

var foundItems = collection.Find(Builders<Item>.Filter.ElemMatch(
                     x => x.StringList,
                     s=>criteria.Contains(s)));

where collection is your IMongoCollection<Item> I see, that you are using FindAll, that means, that your MongoDb driver is of the version 1.x (see here more about it: FindAll in MongoDB .NET Driver 2.0) I would suggest to update your driver, because this version isnot uptodate. Or are the any important reason don't to do it?

This filter query on server. Sure you could get your data as IEnumerable and filter it locally:

var foundItems = collection.Find(x=>true)
                   .ToEnumerable()
                   .Where(x => x.StringList.Intersect(criteria).Any());

If your data is not so huge and you are fine with filtering on client it's a good way too.

And if you are doing already FindAll, that means you get get all the data, you could query it with intersect :

var foundItems = iMongoDataProvider.FindAll<Item>()
                           .Where(x => x.StringList.Intersect(criteria).Any());

这篇关于当列表字段中的值与列表中的条件值匹配时,在 C# 中使用 LINQ 查找 MondoDB 记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆