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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:25:49+00:00 2026-06-14T09:25:49+00:00

quick question, do you know if mongo can create an index on the following

  • 0

quick question, do you know if mongo can create an index on the following data:

{
    prices: {
        price1: 0.90,
        price2: 0.12,
        price3: 0.13
    }
}

so I would like to do index like this ensureIndex({prices:1}) but I am not sure will it include all prices and its values inside.

The reason why I need to lay data this way not the standard Array approach is because I would like to be able to sort based on which price version has been chosen i.e. sort({prices.price:1}).

Any ideas?

cheers

  • 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-14T09:25:51+00:00Added an answer on June 14, 2026 at 9:25 am

    Ok I think I understand now. You want sort all documents in a collection by a given price version.

    This won’t work too well with subdocuments since you can’t do a positional sort as such:

    find({price.version:1}).sort({price.$.version: 1})
    

    As such this would be better as a flat structure within the root document. Infact in your example placing it in a subdocument has no real use other than to group the data which actually makes indexing harder.

    So I would recommend you just project the fields to the root document. You can do something fancy with the aggregation framework here with @Philipp’s answer but it sounds like this query might be run extremely often and maybe on larger working sets.

    As such the new document strucutre would be:

    {
            price1: 0.90,
            price2: 0.12,
            price3: 0.13
    }
    

    This is how I do this kind of stuff in both SQL and MongoDB and it works well with a compound index on all the fields.

    As well since MongoDB is, of course, schemaless you don’t have to worry about maintaining the database when you want to add new prices to your app.

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

Sidebar

Related Questions

A quick question I hope. I would like to know why the line commented
Quick question. I am writing a website and would like to redirect users to
Quick Question: I have the following array (multidimensional): Array ( [preorder] => 0 [data]
Quick question... I have a stupidly long BigInteger which I would like to write
Quick question for everyone: Does anybody know what the actual point size would be
Quick question (quick deadline...I know I can use google). I'm writing a site on
Quick question. When you are accessing a character array, I know you can set
Quick question. Does anyone know if there is a way you can disable the
quick question I know in wordpress you can get post_content,etc but let say I
Just a quick question: Do you know why i can do that: pnrCode =

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.