Aggregate Finding Duplicates When None Exist

Using the following aggregation I’m finding a few duplicate documents inconsistently. Sometimes running the query returns a handful of duplicate documents, sometimes it returns none. They never seem to be the same documents.

db.getCollection("locations").aggregate([
    {
        $addFields: {
            ports: {
                $filter: {
                    input:'$ports',
                    as: 'port',
                    cond: {
                        $and: [
                            { $ne: ['$$port.status', 'removed'},
                            { $ne: ['$$port.status', 'private'] },
                            { $ne: ['$$port.status', 'restricted'] }
                        ]
                    }
                }
            }
        }
    },
    {
        $match: {
            $nor: [
                { ports: { $exists: false } },
                { ports: { $size: 0 } }
            ],
            $or: [
                { _deleted: false },
                { _deleted: { $exists: false } }
            ]
        }
    },
    {
        $group: {
            _id: '$id',
            count: { $sum: 1 }
        }
    },
    { $sort: { count: -1 } }
])

When I check the specific documents that are returned as duplicates, I can see that there is actually only 1 of them in the collection. Also running this simplified aggregation to check for duplicates shows that there is only 1 of each document.

db.getCollection("locations").aggregate([
    {
        $group: {
            _id: '$id',
            count: { $sum: 1 }
        }
    },
    { $sort: { count: -1 } }
])

Is there something wrong with my first query? Any help would be appreciated. I’m at a bit of a loss with this one :slight_smile:

Using MongoDB v6.0.12

I see nothing obvious.

Could you please create a playground with you code and seed it with documents that are problematic?

This way we can experiment with your issue.