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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:55:53+00:00 2026-05-31T08:55:53+00:00

I am setting up a mongoDB db to allow (simple) keyword searching using multikeys

  • 0

I am setting up a mongoDB db to allow (simple) keyword searching using multikeys as recommended here. A record looks similar too:

{ title: { title: "A river runs through", _keywords: ["a","river","runs","through"] ) , ... }

I using nodejs server side, so am using javascript. The following query will match (this was run in the mongo terminal):

> db.torrents_sorted.find({'title._keywords' : {"$all" : ["river","the"]} }).count()
210

However, these do not:

> db.torrents_sorted.find({'title._keywords' : {"$all" : ["/river/i","/the/i"]} }).count()
0

> db.torrents_sorted.find({'title._keywords' : {"$all" : [{ "$regex" : "river", "$options" : "i" },{ "$regex" : "the", "$options" : "i" }]} }).count()
0

Using a single regex (without using $and or $all) does match:

db.torrents_sorted.find({‘title._keywords’ : { “$regex” : “river”, “$options” : “i” } }).count()
1461

Interestingly, using python and pymongo to compile the regular expressions does work:

>>> db.torrents_sorted.find({'title._keywords': { '$all': [re.compile('river'), re.compile('the')]}}).count();
236

I am not necessarily looking for a solution that uses regexes, however it is required that keywords are matched on shorter strings so “riv” matches “river”, which seems ideal for regexes (or LIKE in sql).

My next idea is to try passing in a javascript function that performs the regex matching on the list, or perhaps passing in a seperate function for each regex (this does seem to scream hack at me :), although I’m guessing this would be slower and performance is very important.

  • 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-31T08:55:54+00:00Added an answer on May 31, 2026 at 8:55 am

    Ok, I have an answer, that is kinda interesting in a different way. The bug I was experiencing with regexes exists in version 1.8 of mongodb and has been solved, it is shown here.

    Sadly the hosting company looking after the db atm are not able to offer version 2.0, and the $and keyword was added in version 2.0, although thanks for the debug help Samarth.

    So instead I have written a javascript function to perform the regex matching:

    function () {
      var rs = [RegExp(".*river.*"), RegExp(".*runs.*")];
    
      for(var j = 0; j < rs.length; j++) {
        var val = false;
        for (var i = 0; !val && i < this.title._keywords.length; i++)
          val = rs[j].test(this.title._keywords[i]);
    
        if(!val) return false;
      }
      return true;
    }
    

    This runs in O(n^2) time (not very cool), but will fail in linear time, if the first regex does not match on any on the keywords (since I am looking for a disjunction).

    Any input on optimising this would be greatly appreciated, although if this is the best solution I can find for 1.8, I may have to find somewhere else to store my db in the near future, ;).

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

Sidebar

Related Questions

Any recommended readings for setting up mongodb for sharding/scalability? I'm looking for best practices.
Setting up an integration server, I’m in doubt about the best approach regarding using
Setting onchange event for CheckBoxList using the following code doesn't work. chkListUserGroup.Attributes.Add(onchange, document.forms[0].isRecordModified.value='true';); How
I have recently been getting my feet wet in MongoDB using Mongoid w/ Rails
I am using 10Gen .net driver for MongoDB and querying objects that has empty
I have done a simple experiment to test MongoDB's performance and disk usage. I
I want to use default SLF4J + Logback configuration except setting org.springframework.data.document.mongodb logging level
I am trying to use MongoDB on Ubuntu 11.10 with CakePHP 1.3 using the
Right now, I have configuration setting for morphia talks to one MongoDB from Play!.
I am using 64-bit MongoDB and I had done testing with 64-bit MongoDB. The

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.