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

  • Home
  • SEARCH
  • 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 8490393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:06:13+00:00 2026-06-10T22:06:13+00:00

I’ve got a mongoDB collection that I’ve created via PHP. Now I want to

  • 0

I’ve got a mongoDB collection that I’ve created via PHP. Now I want to index several fields, however the php documentation doesn’t talk about how to not apply a sort to the index!

For example, I want to index field1, field2, field3, and have the whole thing sorted by field3.

Since I will be searching the collection based on field1, field2, I wanted to make sure those have indexes, but when the collection returns from a find() operation, I just wanted it to be sorted by field3.

Any thoughts?

  • 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-06-10T22:06:15+00:00Added an answer on June 10, 2026 at 10:06 pm

    I thought it would be useful for me to give you a bit of background information before fully answering your question:

    When mongo creates an index on a collection it is essentially sorting the collection based on the index key. Thus, when you get the results from a query that uses an index, the results are going to be sorted by key for that index. If your query uses a basic index on field1, then the results are, by definition, sorted on that index.

    There are a few ways to get around this however. One simple way would be to

    1) Query on field1

    2) Sort the result of 1) using field3

    This would, if I understand your comment correctly, get you your desired output. However, it has the downside of requiring 2 major pieces of work, which could be costly depending on the size of your data.

    There is another way to solve your problem: compound indexes. Compound indexes use multiple keys to generate an index that is sorted based on both of those keys. There is some documentation here: http://www.mongodb.org/display/DOCS/Indexes#Indexes-CompoundKeys and there are quite a few stack-overflow questions about compound indexes if you want to dive more deeply into the subject.

    Also, if you want some general info on indexing (and compound indexing) in mongodb check out http://www.mongodb.org/display/DOCS/Indexing+Advice+and+FAQ#IndexingAdviceandFAQ-1.Thesortcolumnmustbethelastcolumnusedintheindex. this whole document should be really useful to you I expect.

    Using compound indexes, while elegant and appropriate, is definitely more complex than just using sort after a query. If you are willing to spend the time looking into it though I am sure you would find it will help you find a nice solution.

    Hope this helps!

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string

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.