The answer from here seems like the same thing, but I’ve tried applying this and it’s not doing what I’d hoped, so clearly I’m doing something wrong.
We have an audit log mongodb database collection that holds business data changes. I am trying to find a distinct list of Services and TableNames that are in scope to create a dropdown on a UI for exploring changes visually. These fields are indexed.
My query looks like this:
db.getCollection("changes")
.aggregate(
{
$group: {"_id": {ServiceName:"$ServiceName", TableName:"$TableName"} }
},
{
$sort: { "ServiceName" : 1, "TableName" : 1 }
},
);
This doesn’t fail, but… the sort doesn’t seem to work. The records come back in a completely random order. It’s like the sort filter is ignored. I’ve tried to use the .sort()
function but I’m told that is not supported.
What is wrong with my query? How do I need to adjust it to get a distinct list of services and tables, ordered by service name and then by table name?