如何使用C#在数组mongoDB中获取子文档

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用C#在数组mongoDB中获取子文档相关的知识,希望对你有一定的参考价值。

这个我的json包含几个属性,我想进行过滤,带来我需要的东西。在Dependentsbenefits我有另一个列表。我很难用LINQ进行过滤。我想只带来包含method参数的ID的字段。

{
    "_id": "b0164f61-a203-478b-becc-b9a67312981d",
    "personalData": { },
    "address": {},
    "emails": [],
    "syndicate": { },
    "dependents": [],
    "phone": [],
    "bank": [],
    "attributes": {},
    "benefits": [],
    "transport": { },
    "attachments": [],
    "documents": [],
    "Dependentsdocuments": [],
    "Dependentsattachments": [],
    "Dependentsbenefits": [
        {
            "_id": "b0164f61-a203-478b-becc-b9a67312981d",
            "id": "5f9bf75a-5c47-4a55-b17b-9f8bd26f3dee",
            "dependentId": "70b39bed-e3e0-41e3-a5be-076623b9a909",
            "type": {
                "_id": "1",
                "label": "Plano de Saúde",
                "name": "healthPlan",
                "required": false
            }
        },
        {
            "_id": "b0164f61-a203-478b-becc-b9a67312981d",
            "id": "0899c807-10de-4ed2-b69a-abe97fe24aa2",
            "dependentId": "70b39bed-e3e0-41e3-a5be-076623b9a909",
            "type": {
                "_id": "2",
                "label": "Plano Odontológico",
                "name": "dentalPlan",
                "required": false
            }
        },
        {
            "_id": "b0164f61-a203-478b-becc-b9a67312981d",
            "id": "7f7927f1-95aa-4ada-a0d6-08e9231977ec",
            "dependentId": "e8139400-da15-4069-aef6-d566e71b4ca0",
            "type": {
                "_id": "1",
                "label": "Plano de Saúde",
                "name": "healthPlan",
                "required": false
            }
        },
        {
            "_id": "b0164f61-a203-478b-becc-b9a67312981d",
            "id": "337fe700-9c58-4f92-964c-096e8137d05a",
            "dependentId": "e8139400-da15-4069-aef6-d566e71b4ca0",
            "type": {
                "_id": "2",
                "label": "Plano Odontológico",
                "name": "dentalPlan",
                "required": false
            }
        },
        {
            "_id": "b0164f61-a203-478b-becc-b9a67312981d",
            "id": "b817b50c-ab43-4c50-824e-c42e3c5afd18",
            "dependentId": "092d6a20-c138-41ff-9a3d-dc38ce23aa6e",
            "type": {
                "_id": "1",
                "label": "Plano de Saúde",
                "name": "healthPlan",
                "required": false
            }
        },
        {
            "_id": "b0164f61-a203-478b-becc-b9a67312981d",
            "id": "2dd47d24-1acc-4b10-9e3c-5da6a2f0245b",
            "dependentId": "092d6a20-c138-41ff-9a3d-dc38ce23aa6e",
            "type": {
                "_id": "2",
                "label": "Plano Odontológico",
                "name": "dentalPlan",
                "required": false
            }
        }
    ]
}

在这个方法GetDocument中。我传递了两个参数,员工ID和依赖。我正在使用这个linq操作和条件。 DependentsDocuments = n.ElementAt (-1) .DependentsDocuments.Where (x => x.dependentId == dependentId) .ToList ()也就是说,它只会在dependentDocument中保存包含依赖项id的那些

public async Task<List<Documents>> GetDocument(string ownerId, string dependentId)
        {

            var findDocument = _employee.AsQueryable().Where(dep => dep.ownerId == ownerId).GroupBy(s => new { s.DependentsDocuments })
                .Select(n => new Employee {
                   DependentsDocuments = n.ElementAt(-1).DependentsDocuments.Where(x => x.dependentId == dependentId).ToList()
                });

            return findDocument.ElementAt(-1).DependentsDocuments;
            
            }

捕获数据的最佳方法是什么?记住我的模型与Json基本相同。关注他们以获得更好的解释。

public class EmployeeViewModel
{
    [BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
    public string ownerId { get; set; }
    public string atributeChange { get;  set; }
    public PersonalDataViewModel personalData { get; set; }
    public AddressViewModel address { get; set; }
    public List<EmailsViewModel>  emails { get; set; }
    public SyndicateViewModel syndicate { get; set; }
    public List<DependentsViewModel> dependents { get; set; }
    public List<PhoneViewModel> phone { get; set; }
    public List<BankViewModel> bank { get; set; }
    public AttributesViewModel attributes { get; set; }
    public List<BenefitsViewModel> benefits { get; set; }
    public TransportViewModel transport { get; set; }
    public List<AttachmentsViewModel> attachments { get; set; }
    public List<DocumentsViewModel> documents { get; set; }
    public List<DocumentsImagesViewModel> DependentsDocuments { get; set; }
    public List<AttachmentsViewModel> DependentsAttachments { get; set; }
    public List<BenefitsViewModel> DependentsBenefits { get; set; }
}

public class DocumentsViewModel
{
    [BsonId]
    public string ownerId { get; set; }
    public string id { get; set; }
    public string dependentId { get; set; }
    public string number { get; set; }
    public DateTime expiration { get; set; }
    public List<DocumentsImagesViewModel> images { get; set; }
    public List<DocumentPropertiesViewModel> properties { get; set; }
    public DocumentTypeViewModel type { get; set; }
}
答案

最好的方法是在数据库端运行此过滤,这样您就可以查找包含$match(对于ownerId)和$filter(对于dependentId)的聚合管道。

在运行查询之前,您应该更改您的类并用Lists替换所有IEnumerable

public IEnumerable<DocumentsViewModel> Dependentsbenefits { get; set; }

这将允许您构造一个将直接转换为MongoDB的Aggregation Framework查询的表达式。尝试:

var query = from employee in Col.AsQueryable()
            where employee.ownerId == ownerId
            select new EmployeeViewModel()
            {
                ownerId = employee.ownerId,
                address = employee.address,
                // you can add more fields here if you need them
                Dependentsbenefits = employee.Dependentsbenefits.Where(x => x.dependentId == dependentId)
            };

var result = query.ToList();

当你运行MongoDB profiler时,你将能够观察到以下查询:

{
    "$match" : {
        "_id" : "b0164f61-a203-478b-becc-b9a67312981d"
    }
},
{
    "$project" : {
        "ownerId" : "$_id",
        "address" : "$address",
        "Dependentsbenefits" : {
            "$filter" : {
                "input" : "$Dependentsbenefits",
                "as" : "x",
                "cond" : {
                    "$eq" : [
                            "$$x.dependentId",
                            "70b39bed-e3e0-41e3-a5be-076623b9a909"
                    ]
                }
            }
        },
        "_id" : 0
    }
}

以上是关于如何使用C#在数组mongoDB中获取子文档的主要内容,如果未能解决你的问题,请参考以下文章

获取数组中每个索引的子文档元素计数并更新子文档键 - 数组中的子文档(IN MONGODB)

如何使用 C# 驱动程序更新 MongoDB 数组中的子文档

MongoDB如何使用包含子文档的自定义键创建一个数组

MongoDB如何使用包含子文档的自定义键创建一个数组

如何匹配MongoDB中的子文档数组?

如何使用 Monk 在 MongoDB 中插入带有子文档数组元素的文档