简体   繁体   English

如何使用 mongodb .net 驱动程序 C# 在集合中查找嵌套数组字段包含特定字符串的项目?

[英]How to find items in a collection that nested array field contains specific string using mongodb .net driver C#?

I have ClassRoom documents in my collection and I have an array of Students in each ClassRoom .我的收藏中有ClassRoom文档,每个ClassRoom中都有一组Students How can I find classes that have students who his/her first name or last name contains a specific string?如何找到学生的名字或姓氏包含特定字符串的班级? Note: I want to implement it using filter builder, not Linq nor aggregation pipeline.注意:我想使用过滤器构建器来实现它,而不是 Linq 也不是聚合管道。

[
  {
    _id: 1,
    name: 'Room1',
    students: 
    [
      {
        _id: 1,
        firstName: 'John Junior',
        lastName: 'Smith'
       },
       {
        _id: 2,
        firstName: 'Jane',
        lastName: 'Jones'
       }
    ]
  },
  {
    _id: 2,
    name: 'Room2',
    students: 
    [
      {
        _id: 3,
        firstName: 'Mike',
        lastName: 'John'
       },
       {
        _id: 4,
        firstName: 'Kate',
        lastName: 'Doe'
       }
    ]
  },
{
    _id: 3,
    name: 'Room3',
    students: 
    [
      {
        _id: 5,
        firstName: 'Johnathan',
        lastName: 'Davis'
       }
    ]
  }
]

eg if we get the string john the query returns all classrooms.例如,如果我们得到字符串john ,查询将返回所有教室。

You can combine an $elemMatch with a $regex, eg:您可以将 $elemMatch 与 $regex 结合使用,例如:

// Define the regex to look for
var regex = new Regex("john", RegexOptions.IgnoreCase);
// Set up the filter
var filter = Builders<Class>.Filter.ElemMatch(
    x => x.Students, 
    Builders<Student>.Filter.Or(
        Builders<Student>.Filter.Regex(x => x.FirstName, regex),
        Builders<Student>.Filter.Regex(x => x.LastName, regex)
    ));
// Read results
var result = await (await classes.FindAsync(filter)).ToListAsync();

Using a regex is not the best option in terms of performance;就性能而言,使用正则表达式并不是最佳选择; if it is too slow, you can also have a look at full-text-search.如果太慢,你也可以看看全文搜索。 For details see this link .有关详细信息,请参阅此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 C# Mongodb 驱动程序在嵌套列表中查找项目 - Find items in nested list using C# Mongodb driver 如何使用 C# 驱动程序 2.10.4 查找 MongoDB 中特定字段的最小值 - How to find the min value of a specific field in MongoDB using C# driver 2.10.4 如何使用 Mongodb c# 驱动程序更新文档的数组字段的特定索引值? - How to update document's array field's specific indexed value using Mongodb c# driver? 如何使用C#驱动程序在mongodb嵌套数组中搜索 - How to search in mongodb nested array using c# driver 使用C#在mongodb中的嵌套数组中选择特定字段 - select specific field in nested array in mongodb using c# C# MongoDb Driver Filter 使用集合中的嵌套属性 - C# MongoDb Driver Filter using nested property from collection MongoDB 驱动程序 C# 更新嵌套对象数组中的单个字段 - MongoDB Driver C# Update a single field in Nested Array of Objects 如何在 MongoDb C# 驱动程序的嵌套集合中进行批量更新? - How to make bulk upsert in nested collection in MongoDb C# Driver? MongoDb c#driver按字段值查找数组中的项目 - MongoDb c# driver find item in array by field value 使用具有给定字符串数组的 MongoDB C# 驱动程序在嵌套数组上使用过滤器生成器进行查询 - Query with filter builder on nested array using MongoDB C# driver with a given array of string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM