1

Using query aggregation I want to create a new array by a filter of another array, so that the filtered result will be done by a specific field of the preliminary array. In this case, I want to filter by the field "fieldName".

I will always want to filter out the last occur

example: I have one document:

{
"fullyQualifiedName" : "MongoDB",
"items" : [ 
    {
        "fieldName" : "_id",
        "fieldCount" : 7,
        "confidence_level" : 1,
        "fieldClassifications" : [ 
            "LineageGuid"
        ],
    }, 
    {
        "fieldName" : "_id",
        "fieldCount" : 7,
        "fieldClassifications" : [ 
            {
                "classificationName" : "LineageGuid",
            }
        ]
    }, 
    {
        "fieldName" : "details",
        "fieldCount" : 7,
    }, 
    {
        "fieldName" : "state",
        "fieldCount" : 7,
    }
]

}

I want to create a new array like:

"items" : [ 
    {
        "fieldName" : "_id",
        "fieldCount" : 7,
        "confidence_level" : 1,
        "fieldClassifications" : [ 
            "LineageGuid"
        ],
    }, 
    {
        "fieldName" : "details",
        "fieldCount" : 7,
    }, 
    {
        "fieldName" : "state",
        "fieldCount" : 7,
    }
]

The simple solution is to $unwind and $group again but I can't do it because of performance issue. I am using MongoDB 3.4

1
  • Is there anything now working in the anwer? If "yes" then please let know the issue. Commented Nov 8, 2018 at 11:40

1 Answer 1

1

You can use below aggregation

db.collection.aggregate([
  { "$addFields": {
    "items": {
      "$map": {
        "input": {
          "$setUnion": [
            { "$map": {
              "input": "$items",
              "in": { "$indexOfArray": ["$items.fieldName", "$$this.fieldName"] }
            }}
          ]
        },
        "as": "i",
        "in": {
          "fieldName": { "$arrayElemAt": ["$items.fieldName", "$$i"] },
          "fieldCount": { "$arrayElemAt": ["$items.fieldCount", "$$i"] },
          "confidence_level": { "$arrayElemAt": ["$items.confidence_level", "$$i"] },
          "fieldClassifications": { "$arrayElemAt": ["$items.fieldClassifications", "$$i"] }
        }
      }
    }
  }}
])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.