如何仅获取数组MongoDb中的特定列表-C#

时间:2019-04-18 12:41:16

标签: c# asp.net mongodb

我只需要将包含即将到来的相等参数的列表放在最前端

如何进入Dependentsbenefits数组,仅列出包含dependentId等于"70b39bed-e3e0-41e3-a5be-076623b9a909"的列表

{
    "_id": "b0164f61-a203-478b-becc-b9a67312981d",
    "personalData": {
        "_id": "b0164f61-a203-478b-becc-b9a67312981d",
        "name": "asdasdsad",
        "viewName": "asdasdsad",
        "gender": "Masculino",
        "birthDate": "1997-12-30T02:00:00.000Z",
        "birthState": "Acre",
        "birthCity": "Afonso Cláudio"
    },
    "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
            }
        }
    ]
}

我在下面尝试使用此方法,但是我的反馈必须为public async Task<List<Documents>> GetDocument(string ownerId, string dependentId)

public async Task<List<Documents>> GetDocument(string ownerId, string dependentId)
        {
            var query = from doc in _employee.AsQueryable()
                        where doc.ownerId == ownerId && doc.DependentsDocuments.Any(dep => dep.dependentId == dependentId)
                        select new Documents()
                        {
                            ownerId = doc.DependentsDocuments.ElementAt(-1).ownerId,
                            id = doc.DependentsDocuments.ElementAt(-1).id,
                            dependentId = doc.DependentsDocuments.ElementAt(-1).dependentId,
                            number = doc.DependentsDocuments.ElementAt(-1).number,
                            expiration = doc.DependentsDocuments.ElementAt(-1).expiration,
                            images = doc.DependentsDocuments.ElementAt(-1).images,
                            properties = doc.DependentsDocuments.ElementAt(-1).properties,
                            type = doc.DependentsDocuments.ElementAt(-1).type,
                        };
         
            return query;
        }

1 个答案:

答案 0 :(得分:1)

在查询级别,您可以尝试MongoDB查询:

db.collection.aggregate([
    {
        $project: {
            "_id":1,
            "personalData":1,
            "Dependentsbenefits": {
                $filter: {
                    input: "$Dependentsbenefits",
                    as: "depend",
                    cond:  { $eq: ["$$depend.dependentId", "70b39bed-e3e0-41e3-a5be-076623b9a909"] }
                }
            }
        }        
    }
]) 

结果将如下所示:

{
    "_id" : "b0164f61-a203-478b-becc-b9a67312981d",
    "personalData" : {
        "_id" : "b0164f61-a203-478b-becc-b9a67312981d",
        "name" : "asdasdsad",
        "viewName" : "asdasdsad",
        "gender" : "Masculino",
        "birthDate" : "1997-12-30T02:00:00.000Z",
        "birthState" : "Acre",
        "birthCity" : "Afonso Cláudio"
    },
    "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
            }
        }
    ]
}