使用C#在MongoDB集合中的嵌入式文档的子句查询中的投影

用户名

在数据库而不是内存中过滤集合

我有一个模型类,将其保存在MongoDB集合中,然后按照以下我的期望进行查询。

我的模特班:

public Class Employee
{
    public ObjectId Id { get; set; }
    public string EmpID { get; set; }
    public string EmpName { get; set; }
    public List<Mobile> EmpMobile { get; set; }
    public bool IsLive { get; set; }
}

public Class Mobile
{
    public string MobID { get; set; }
    public string MobNumber { get; set; }
    public bool IsPreferred { get; set; }
    public bool IsLive { get; set; }
}

值是

List<Employee> EmpInfo = new List<Employee>() {
new Employee()
{
    EmpID = "100",
    EmpName = "John",
    EmpMobile = new List<Mobile>()
    {
        new Mobile() { MobNumber = "55566610", IsPreferred = true, IsLive = false },
        new Mobile() { MobNumber = "55566611", IsPreferred = false, IsLive = true },
    },
    IsLive = true
},

new Employee()
{
    EmpID = "101",
    EmpName = "Peter",
    EmpMobile = new List<Mobile>()
    {
        new Mobile() { MobNumber = "55566610", IsPreferred = true, IsLive = false },
        new Mobile() { MobNumber = "55566611", IsPreferred = false, IsLive = false },
    },
    IsLive = true
},

new Employee()
{
    EmpID = "102",
    EmpName = "Jack",
    EmpMobile = new List<Mobile>()
    {
        new Mobile() { MobNumber = "55566610", IsPreferred = true, IsLive = true },
        new Mobile() { MobNumber = "55566611", IsPreferred = false, IsLive = true },
    },
    IsLive = false
}

}

collectionEmpInfo.InsertMany(EmpInfo);
var empList = collectionEmpInfo.Find(new BsonDocument()).ToList();

现在,我希望仅EmpInfo.IsLive == true在嵌入式文档中选择“我只需要EmpInfo.EmpMobile.IsLive == true满足的移动文档”

我的预期输出:

List<Employee> EmpInfo = new List<Employee>() {
new Employee()
{
    EmpID = "100",
    EmpName = "John",
    EmpMobile = new List<Mobile>()
    {
        new Mobile() { MobNumber = "55566611", IsPreferred = false, IsLive = true }
    },
    IsLive = true
},

new Employee()
{
    EmpID = "101",
    EmpName = "Peter",
    EmpMobile = new List<Mobile>()
    {

    },
    IsLive = true
}

}

请协助我如何使用c#MongoDB为期望的输出编写Where子句查询

注意:在数据库而不是内存中过滤集合

我的MongoDB库和连接是

IMongoClient _client = new MongoClient();
IMongoDatabase _database = _client.GetDatabase("Test");
教授79

编辑

已添加projection-因此所选数组仅包含文档,其中IsLive==true

我认为使用类型查询和使用c#强类型语言比较容易我使用ElemMatch它的目的是扫描阵列并寻找匹配的元素。

var filterDef = new FilterDefinitionBuilder<Employee>();
var filter = filterDef.Eq(x => x.IsLive, true);

var projectDef = new ProjectionDefinitionBuilder<Employee>();
var projection = projectDef.ElemMatch<Mobile>("EmpMobile", "{IsLive:true}");            

var empList = collectionEmpInfo.Find(filter).Project<Employee>(projection).ToList();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

查询MongoDB中的嵌入式文档

使用MongoDB和C#新驱动程序版本(2.0)更新集合中的嵌入式文档

基于范围查询MongoDB中嵌入式文档的数组

如何在 MongoDb 中查询嵌入式文档?

查询Spring Data MongoDB中嵌入式文档的数组

Mongodb C#驱动程序Perform字符串包含对嵌入式文档中的属性的查询

如何在嵌入式数组mongodb中查询嵌入式文档

在mongodb中展开嵌入式文档

在 MongoDB 中搜索嵌入式文档?

使用python(pymongo)在mongodb中编辑嵌入式文档

MongoDB C#驱动程序-更新嵌入式文档数组中的所有字段

在文档中引用文档的嵌入式数组的嵌入式数组上查询Doctrine2和MongoDB

使用Presto查询MongoDB嵌入式/嵌套文档的数组

如何在MongoDB中查询数组中的单个嵌入式文档?

在mongoDb中对嵌入式文档数组(3个级别)进行过滤查询

php:将mongodb嵌入式文档查询到数组中

如何使用C#仅从多层嵌入式MongoDB文档中获取具有相应父元素的确切子元素

Mongo查询嵌入式文档中的动态字段

查询MongoEngine中的嵌入式文档列表

Mongoid在单个查询中推送新的嵌入式文档

使用Mongoose从Node从MongoDB中的复杂嵌入式集合中插入,更新和删除

更新mongodb中arrays对象的arrays的嵌入式文档

mongodb聚合中的项目嵌套嵌入式文档

更新Mongodb中的嵌入式文档:性能问题?

如何在mongodb中搜索嵌入式文档?

嵌入式文档数组中的MongoDB Aggregate ObjectId

在mongodb中更新嵌套的嵌入式文档

映射-Symfony2中的Mongodb嵌入式文档

使用C#获取和添加/更新多层嵌入式/嵌套MongoDB文档