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 6356301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:56:13+00:00 2026-05-24T22:56:13+00:00

Is that possible to sort data in sub array in mongo database? { _id

  • 0

Is that possible to sort data in sub array in mongo database?

{   "_id" : ObjectId("4e3f8c7de7c7914b87d2e0eb"), 
    "list" : [
            {
                "id" : ObjectId("4e3f8d0be62883f70c00031c"),
                "datetime" : 1312787723,
                "comments" : 
                    {
                        "id" : ObjectId("4e3f8d0be62883f70c00031d")
                        "datetime": 1312787723,
                    },
                    {
                        "id" : ObjectId("4e3f8d0be62883f70c00031d")
                        "datetime": 1312787724,
                    },                  
                    {
                        "id" : ObjectId("4e3f8d0be62883f70c00031d")
                        "datetime": 1312787725,
                    },
            }
        ], 
    "user_id" : "3" }

For example I want to sort comments by field “datetime”. Thanks. Or only variant is to select all data and sort it in PHP code, but my query works with limit from mongo…

  • 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-24T22:56:14+00:00Added an answer on May 24, 2026 at 10:56 pm

    With MongoDB, you can sort the documents or select only some parts of the documents, but you can’t modify the documents returned by a search query.

    If the current order of your comments can be changed, then the best solution would be to sort them in the MongoDB documents (find(), then for each doc, sort its comments and update()). If you want to keep the current internal order of comments, then you’ll have to sort each document after each query.

    In both case, the sort will be done with PHP. Something like:

    foreach ($doc['list'] as $list) {
        // uses a lambda function, PHP 5.3 required
        usort($list['comments'], function($a,$b){ return $a["datetime"] < $b["datetime"] ? -1 : 1; });
    }
    

    If you can’t use PHP 5.3, replace the lambda function by a normal one. See usort() examples.

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

Sidebar

Related Questions

I know that JTable can sort by a single column. But is it possible
Is that possible to connect mysql database between servers. I mean, lets say i
How is it possible to sort data from multiple tables by relevance? My table
Is it possible to add some sort of meta data to a view to
Most projects have some sort of data that are essentially static between releases and
Possible Duplicate: PHP Sort a multidimensional array by element containing date I have some
Hi is it possible to cast a List? i have an abstract class that
I have two databases that are differently formatted. Each database contains person data such
Is it possible to sort a multidimensional array by multiple columns using natural sort
Is that possible to switch an Open Source Project license from GPL to LGPL

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.