How should I do this without collation?

I’m evaluating an Atlas serverless setup, but unfortunately, it doesn’t support collation (has it been stated whether adding support is on the roadmap?).

I’ve got an array of subdocuments. If a user edits one of the subdocuments’ name field, I want it to be re-sorted. I’m using the following routine to do so:

await self._async_collection.update_one(
        "$push": {
            "macros": {
                "$each": [],
                "$sort": {"name": 1},
    collation={"locale": "en"},

I know that I could sort the array in application code and use $set to replace the entire array, but before I resort to that, I want to know if there is another method I should try first. I’m using collation because I need a case-insensitive sort.

Hi @Jared_Lindsay1,

Depending on the use case and If the content of the array both before & after the update is known to the application end, it may be more efficient from a performance perspective to push the sorted array from the application side instead of sorting in the database.

In saying so, you can possibly consider $toLower , $sort and $merge usage but this would be more complex and generally more resource intensive than the application side sorting. Additionally, this would lead to changes in the string values of the field you want to sort (in which $toLower would lower case the characters) so this is another factor to consider. Although I would also note the serverless operational limitations and considerations as well.

If further assistance is required, please provide some sample documents and expected/desired output.

Please let me know your thoughts.


Thanks for the reply. I wound up sorting in application code and pushing the whole new array via $set. In another place where I was inserting to the array, I used Python’s bisect module to find the insertion index, then $pushed to that index (as well as using list.insert() in the app code).

Thanks for updating the post with your solution Jared! :slight_smile: Glad to hear you decided on a method you’ve found suitable.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.