1

I am trying to do a simple wildcard query in a MongoDB database using a user-generated string variable. For example, if the user searches 'Bu', things like 'Burger' and 'Burger King' should be returned from the database. I have searched and tried several things but nothing seems to work. Any help would be greatly appreciated. Note: This is client-side JS.

    var text = document.getElementById("Search_Box").value;

    var regex = new RegExp(text + ".*");

    client.login().then(() => db.collection('businesses')
        .find({name:{$regex:regex}}).then(docs => {
          console.log("[MongoDB Stitch] Connected to Stitch");
2
  • As opposes to something like Node. I just wanted to be clear because sometimes they work differently. Commented Nov 17, 2017 at 20:32
  • Not meteor. Standard js Commented Nov 17, 2017 at 20:40

2 Answers 2

5

If you had the following documents:

{
    "_id" : ObjectId("5a0f3a464d8a2fe38bec4e92"),
    "name" : "Burger"
}

{
    "_id" : ObjectId("5a0f3a464d8a2fe38bec4e94"),
    "name" : "Burger King"
}

{
    "_id" : ObjectId("5a0f3a464d8a2fe38bec4e96"),
    "name" : "Booby's"
}

{
    "_id" : ObjectId("5a0f3a464d8a2fe38bec4e98"),
    "name" : "McDonald"
}

Starting with

To get everything starting with "Bu" you could do

db.collection('businesses').find({name: {$regex: '^Bu'}})

or

db.collection('businesses').find({name: {$regex: /^Bu/}})

In the middle of

If you needed anything that contained "Ki.*g" anywhere in the word you could do:

db.collection('businesses').find({name: {$regex: 'Ki.*g'}})

or

db.collection('businesses').find({name: {$regex: /Ki.*g/}})

Do the effort and go through the documentation. Everything is explained there, with a lot more details. https://docs.mongodb.com/manual/reference/operator/query/regex/

Sign up to request clarification or add additional context in comments.

2 Comments

I understand how to do regex when you hardcode it as you did. My problem is using a string variable because regex does not accept strings.
then why don't you build the regex in a different variable and pass it in your RegExp() constructor? var stringRegex = text + ".*"; and then new RegExp(stringRegex);. Are we talking about the same RegExp? developer.mozilla.org/de/docs/Web/JavaScript/Reference/…
-1

I'm having a similar problem. I'm sending:

async search (term) {
  this.suggestions = await this.db.collection(this.collection).find({name: {$regex: term + '.*', $options: 'i'}}).sort({'_id': 1}).execute()
}

And I'm getting this back from Mongo

Stack Trace:
StitchError: $regex requires regular expression
at find (<native code>)
at apply (<native code>)
at executeServiceFunction (<anonymous>:10:10)
{
  "service": "mongodb-atlas",
  "name": "find",
  "arguments": [
    {
      "database": "monsters",
      "collection": "monsters",
      "query": {
        "name": {
          "$regex": "Aart.*",
          "$options": "i"
        }
      },
      "project": null,
      "sort": {
        "_id": {
          "$numberInt": "1"
        }
      }
    }
  ]
}

1 Comment

Please use the answers section to post answers. If you have a comment you would like to make, use the comments section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.