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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:41:15+00:00 2026-05-30T11:41:15+00:00

I have a model pretty much like this: Document Release has an embedded array

  • 0

I have a model pretty much like this:

Document Release

  • has an embedded array ReleaseDetails[]

  • The ReleaseDetails array contains documents of type ReleaseDetails

  • A ReleaseDetails document has a field called ArtistName of type text

  • A ReleaseDetails document has a field called Type of type text

I basically want to do this:

retrieve all the Release documents that have an entry in their ReleaseDetails array which (both) has ArtistName=someRegexExpression AND Type=someOtherRegexExpression. Basically I do this:

db.getCollection("releasesCollection").
          find({ "ReleaseDetails" : 
                 { "$elemMatch" : 
                      { "ArtistName" : {$regex:"^David"},
                        "Type" : {$regex:".*singer.*"}}})

Problem is, if I call explain() on such a query I can see that the indexes I’ve made on

ReleaseDetails.ArtistName and ReleaseDetails.Type are effectively not taken into account (the query just goes through all documents in the collection).

On the other hand, if I do the exact same query but replace the regex expressions with actual values, in other words, if I do this:

db.getCollection("releasesCollection").
          find({ "ReleaseDetails" : 
                 { "$elemMatch" : 
                      { "ArtistName" : "David Halliday",
                        "Type" : "mainSinger"}}})

in this case the indexs ARE taken into account (explain() show that clearly).

My question is then, is there a way to have a query that does $elemMatch WITH regex take advantage of the indexes?

(I’m asking because I’ve also seen that in fact, if you do a regex query on a basic field (like a text field, not an embedded-array field) AND that field is indexed, my regex query will infact take advantage of the indexes. Why is it that regex query on a basic indexed fields uses the index but regex query on an embedded-array indexed field fails to use the indexes?)

  • 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-30T11:41:17+00:00Added an answer on May 30, 2026 at 11:41 am

    Two important things you may missed:

    1.Only case sensitive prefix regexp can use index in mongodb, all others – can’t.

    For example following query will use index:

    db.users.find({ "name": /^andrew/ })

    2.Any query can use only one index per query, therefore it will be better to create compound index for your query:

    db.items.ensureIndex({"ReleaseDetails.ArtistName": 1, "ReleaseDetails.Type" : 1});
    

    And to take advantages of mongodb indexes you should not use like regexp -> "Type" : {$regex:".*singer.*"} (probably because of this regexp your query not use index).

    If you really need like search that you can tokenize yourself Type and store it as an array. For example:

    If you have following Type: “My favorite singer” you can:

    1. Split this phrase into words and store in lower case: [my, favorite, singer]
    2. Tokenize words that’s bigger then 3 chars to use like search like this:
      [my, fav, favo, favor, favori, favorit, favorite, avorite, vorite, orite, rite, ite,
      avorit, vori]
      (i’ve skipped singer word tokenizing)
    3. After such tokenizing done you can search by exact match and your query will use index, but your database for sure will be bigger ;).

    About algorithms how to tokenize words you can read from full text search engines like lucene, sphinx

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

Sidebar

Related Questions

I like pretty much of the localization model in asp.mvc. You have the resx-fallback
I'm probably totally betraying my ignorance at this point, but I have pretty much
I have a star ratings feature. And pretty much, I would like it to
I have a pretty generic Article model, with m2m relation to Tag model. I
I have model Foo which has field bar. The bar field should be unique,
I have model Article it has field title with some text that may contain
I have a model, Thing, that has a has_many with ThingPhoto, using Paperclip to
I'm pretty much a newb with spring-hibernate and I have been trying to make
I have a model like so: return new MyViewModel() { Name = My View
I have this domain model: class Person < ActiveRecord::Base composed_of :address, mapping: [%w(address_street street),

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.