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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:12:43+00:00 2026-05-30T00:12:43+00:00

I am trying to output users chat history and then put it into a

  • 0

I am trying to output users chat history and then put it into a JSON object I want the messages to come out in descending order but when I try $cursor->sort() is throwing Call to a member function sort() on a non-object

$cursor = $collection->findOne(array('chatbetween' => $channel_name));
$cursor->sort(array('messages' => -1));
$messages = array();

for($i=0; $i<count($cursor['messages']); $i++){
    $object = array('message'=>$cursor['messages'][$i]['message'],
                    'time'=>date('Y-m-d\TH:i:s\Z', $cursor['messages'][$i]['time']->sec),
                    'user'=>$cursor['messages'][$i]['user']);

    $messages[] = $object;
}
echo json_encode($messages);

Here is what a collection looks like.

"_id": ObjectId("4f3c19e37edae1723d000000"),
   "chatbetween": "private-4f3bb96d7edae1850b0000004f3c0d2d7edae1e935010000",
   "messages"▼: {
      "0": {
       "user": "4f3c0d2d7edae1e935010000",
       "time": ISODate("2012-02-15T20: 47: 30.175Z"),
       "message": "message1"
     },
      "1": {
      "user": "4f3bb96d7edae1850b000000",
      "time": ISODate("2012-02-15T20: 47: 37.79Z"),
      "message": "message2"
    },
      "2": {
      "user": "4f3c0d2d7edae1e935010000",
      "time": ISODate("2012-02-15T20: 47: 43.295Z"),
      "message": "message3"
   }
}
  • 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-30T00:12:45+00:00Added an answer on May 30, 2026 at 12:12 am

    Your problem is you’re trying to sort sub-documents, whereas the .sort() function is for sorting documents.

    So, each object in the collection is a document–in this case, each chat is a document. You can sort your chats with .sort(), but you have subdocuments called messages within your documents (sub-documents), and Mongo doesn’t sort those for you.

    See some answers to a similar question here:
    Sort Sub Documents in MongoDB

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

Sidebar

Related Questions

I am trying to output JSON directly in my Razor view. The object being
I am trying to output a lot of data (membership record) into a JTextArea,
I'm trying to output unicode string into RTF format. (using c# and winforms) From
Trying to: get all users by using the query in the below script into
I am trying to allow users to enter Hebrew characters into certain fields in
I'm trying to have users enter info into a form (via radio buttons), manipulate
Im trying to get this program to take the users input and put that
Im trying to print the realtime output based on user input for a search.
I am trying to output the value of x in the following request: http://localhost:4827/Default.aspx?x=123123
I am trying to output the comments of a facebook page in PHP. For

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.