0

When I query my database with:

{
    "metadata.text": "Hello world"
}

I get:

{
    "_id": {
        "$oid": "5a455574d93b6a44cd95b210"
    },
    "metadata": {
        "text": "Hello world"
    }
}

I want to modify the query so that it matches anything with 'world'. When I try:

{
    "metadata.text": {
        "$regex": "/world/"
    }
}

I get no matches.

How do I modify my query so that it matches any text containing 'world'?

1 Answer 1

1

Do this instead:

{
    "metadata.text": {
        "$regex": /world/
    }
}

Or

{
    "metadata.text": {
        "$regex": ".*world.*"
    }
}

If you know that your word is at the end then you can also do this:

{
    "metadata.text": {
        "$regex": /world$/
    }
}

Or

{
    "metadata.text": {
        "$regex": ".*world$"
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm trying to run the query through mlab.com and I get the following: 'We encountered an error while parsing your JSON. Please check your syntax (e.g. ensure you are using double quotes around both your field names and values) and try again'
@Mary I added both ways you can write it, check out the update

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.