使用C#在MongoDB集合中的嵌入式文档的子句查询中的投影 [英] Projection in Where Clause Query of a Embedded document in MongoDB Collection using C#

查看:105
本文介绍了使用C#在MongoDB集合中的嵌入式文档的子句查询中的投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Filter the Collection in DB instead of Memory

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

I'm having a Model Class, Save it in a MongoDB Collection then Query the same as per my expectation mentioned below.

我的模型班:

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 个满意的移动文档

Now I wish to Select Only EmpInfo.IsLive == true inside the embedded document I need only EmpInfo.EmpMobile.IsLive == true satisfied Mobile documents

我的预期输出:

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子句查询.

Kindly assist me how to write a Where Clause Query for my expected output using c# MongoDB.

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

Note: Filter the Collection in DB instead of Memory

我的MongoDB库和连接为

My MongoDB Libraries and Connections are

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

推荐答案

EDIT

EDIT

添加了projection-因此所选数组仅包含IsLive==true

Added projection - so selected array contains only documents where IsLive==true

我认为使用类型查询比较容易,因为c#是强类型语言. 我使用了ElemMatch,因为它旨在扫描数组并寻找匹配的元素.

I think it is easier to use typed queries as c# is strongly typed language. I used ElemMatch as this is designed to scan an array and looks for a matching element.

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();

这篇关于使用C#在MongoDB集合中的嵌入式文档的子句查询中的投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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