In MongoDB, what’s the most efficient approach to sort an array in a document and then store the sorted array back to the document?
For background, I have a process that generates a collection of time stamped data and stores it to the DB, however it’s not guaranteed to be stored in time-sequential order. Once the data collection is complete, I’d like to sort the array of data by time and write it back to the DB since all of the future queries on the data will always require sorting by time.
Thanks!
There’s no in-place array sorting in MongoDB. Read that array into PHP (or whatever), sort it there and save back.
When you later will read that array, it will stay sorted.