Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:54:49+00:00 2026-05-28T22:54:49+00:00

In MongoDB, what’s the most efficient approach to sort an array in a document

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:54:49+00:00Added an answer on May 28, 2026 at 10:54 pm

    There’s no in-place array sorting in MongoDB. Read that array into PHP (or whatever), sort it there and save back.

    db.collection.update({_id: document_id}, {$set: {myarray: [3, 2, 1]}});
    

    When you later will read that array, it will stay sorted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In MongoDB, a field can have multiple values (an array of values). Each of
In MongoDB, I have a document with a field called ClockInTime that was imported
In MongoDB I have a collection of documents called 'clients', where each document is
Using mongodb with the NoRM driver I have this document: { _id : ObjectId(0758030341b870c019591900),
I am using mongodb to store user's events, there's a document for every user,
Using MongoDB... If I have an array of ids in one field, can I
I have launch mongodb server: [demas@arch.local.net][~]% mongod --dbpatmongod --dbpath /home/demas/temp/ Mon Apr 19 09:44:18
I'm building a Rails application using MongoDB as the back-end and MongoMapper as the
I'm using MongoDB to hold a collection of documents. Each document has an _id
In MongoDB, I have documents defined like: { 'foo': { 'a': [1,2,3,4,5], 'b': [1,5,9],

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.