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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:07:21+00:00 2026-05-28T16:07:21+00:00

Ok i am using mongo db and have a repeating region on my page:

  • 0

Ok i am using mongo db and have a repeating region on my page:

try {

    $connection = new Mongo();
    $database   = $connection->selectDB($selectDB);
    $collection = $database->selectCollection($selectCollection);

} catch(MongoConnectionException $e) {
    die("Failed to connect to database ".$e->getMessage());
}

$cursor = $collection->find();

while ($cursor->hasNext()): $document = $cursor->getNext(); 


                        echo $document['fieldName']."<br/>";
                        echo $document['fieldType']."<br/>";
                        echo $document['fieldLength']."<br/>";
                        echo $document['user_id']."<br/>";
                        echo $document['order']."<br/>";
                        echo "<hr/>";

 endwhile;

this works fine but what i am trying to do now is sort by user_id. I have tried this:

 try {

    $connection = new Mongo();
    $database   = $connection->selectDB($selectDB);
    $collection = $database->selectCollection($selectCollection);

} catch(MongoConnectionException $e) {
    die("Failed to connect to database ".$e->getMessage());
}

$cursor = $collection->find().sort({user_id: -1});

while ($cursor->hasNext()): $document = $cursor->getNext(); 


                        echo $document['fieldName']."<br/>";
                        echo $document['fieldType']."<br/>";
                        echo $document['fieldLength']."<br/>";
                        echo $document['user_id']."<br/>";
                        echo $document['order']."<br/>";
                        echo "<hr/>";

 endwhile;

The line i altered is the: $cursor = $collection->find().sort({user_id: -1});

i am getting php error when id o this. Can someone show me the proper syntex to get this array to sort.

I have also tried:

$cursor = $collection->find()->sort({user_id: -1});

and 

 $cursor = $collection->find();
 $cursor = $cursor.sort({user_id: -1});

Any help would be appreciated. thanks.

answer found*

$cursor = $collection->find();
$cursor->sort(array('user_id' => 1));
  • 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-28T16:07:22+00:00Added an answer on May 28, 2026 at 4:07 pm

    I think this is a PHP syntax error:

    $collection->find().sort({user_id: -1})
    

    is invalid PHP. In PHP, the dot operator (“.“) is used to concatenate strings, not to access object members. For that, use the arrow operator (“->“). Additionally, “{user_id: -1}” is not correct syntax for an associative array in PHP. For that, use “array("user_id" => -1)“.

    This gives us:

    $collection->find()->sort(array("user_id" => -1))
    

    which I believe should work.

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

Sidebar

Related Questions

How can I have one mongo model go to one database and another using
I have got mongo db using only one collection and consuming average cpu usage
I have been using mysql in a new rails application, but now I wanted
I have a site that I'm using Mongo on. So far everything is going
I have to read an existing mongo database. Is mongoid suitable for this? All
I am new to Mongo DB I have to implement it in java. I
I started using NoSQL with Ruby. Here's the code I have require 'mongo' require
So I am starting out using the play framework with Mongo and have an
I have a Mongo collection called documents which has the following structure: { name:
I have some code that deletes multiple objects from Mongo using an $or opperator,

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.