Array graphic documents in mongodb collection

I have a collection of objects like this:

 {
        "_id" : ObjectId("55f93cedc4fd0e1f309aea64"),
        "entityType" : "1",
        "identifierIdentity" : [
                {
                        "identifierTypeCode" : "NPI",
                        "identifierValue" : "111"
                },
                {
                        "identifierTypeCode" : "NPI",
                        "identifierValue" : "123"
                },
                {
                        "identifierTypeCode" : "NPI",
                        "identifierValue" : "141"
                },
                {
                        "identifierTypeCode" : "SSN",
                        "identifierValue" : "155"
                }
        ]
}
{
        "_id" : ObjectId("55f93cedc4fd0e1f309aea65"),
        "entityType" : "2",
        "identifierIdentity" : [
                {
                        "identifierTypeCode" : "NPI",
                        "identifierValue" : "111"
                },
                {
                        "identifierTypeCode" : "NPI",
                        "identifierValue" : "123"
                },
                {
                        "identifierTypeCode" : "SSN",
                        "identifierValue" : "155"
                }
        ]
}
{
        "_id" : ObjectId("55f93cedc4fd0e1f309aea66"),
        "entityType" : "3",
        "identifierIdentity" : [
                {
                        "identifierTypeCode" : "SSN",
                        "identifierValue" : "111"
                },
                {
                        "identifierTypeCode" : "SSN",
                        "identifierValue" : "123"
                }
        ]
}

in the identifier above. Identity is an array of documents.

I am trying to get an object counter where "identifierTypeCode": "NPI" is greater than or equal to two.

I can get this with java code, but I have millions of records that take a lot of time. I want to know how much I can achieve this in one query.

+4
source share
1 answer

The aggregation operator will do a short job combined with a match for arrays that have at least two elements to trim down: $redact

    db.collection.aggregate([
    { "$match": {
        "identifierIdentity.identifierTypeCode": "NPI",
        "identifierIdentity.1": { "$exists": true }
    }},
    { "$redact": {
        "$cond": {
            "if": { 
                "$gte":  [ 
                    { "$size": { "$setDifference": [
                        { "$map": {
                            "input": "$identifierIdentity",
                            "as": "el",
                            "in": {
                                "$cond": {
                                    "if": { "$eq": ["$$el.identifierTypeCode", "NPI"] },
                                    "then": "$$el",
                                    "else": false
                                }
                            }
                        }},
                        [false]
                    ] } },
                    2
                ]
            },
            "then": "$$KEEP",
            "else": "$$PRUNE"
        }
    }}
])

, , $redact , . 2, .

0

Source: https://habr.com/ru/post/1607621/


All Articles