0

I am trying to query a collection and want to do it on just the beginning of the postal code. I've tried following the instructions but get 0 documents:

> db.listings.count({"house.postalcode" :  "EH21 8RG"})
2
> db.listings.count({"house.postalcode" :  {"$regex" : "/^EH21/"}})
0

I know I must be missing something but I've checked the regex online and you can see the results above of the two queries.. help!

1
  • When using $regex remove either remove string quotes (to make it a regex expression) and use : {"$regex" : /^EH21/} or use the string quotes without the forward slashes: {"$regex" : "^EH21"} docs Commented Aug 14, 2020 at 9:25

1 Answer 1

1

It probably depends on what driver/library you use, but this works for me:

db.listings.count({ "house.postalcode": /^EH21/ })
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, thank you! :-) > db.listings.count({"house.postal_code" : /^EH21/}) 471

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.