0

i have a collection {mobile:123456789},{mobile:8877887799},{mobile:8899445588}so here i want to search patter to get matched field.

db.users.find({"$or":[{"mobile":/^88.*/}]}) if i did this code to search and want to get matched field "two". how can i do this.
prat_raj21
  • 11
  • 5
  • regex pattern matching strings in queries. Not number... – zangw Mar 10 '16 at 07:34
  • Then you're plain out of luck, since regular expressions only works on "strings", and **everywhere**. JavaScript stringifies when subjected to a regex test. So you could always use `$where`, but it's not efficient and basically scans the whole collection. – Blakes Seven Mar 10 '16 at 07:34
  • any other option to get efficient integer search – prat_raj21 Mar 10 '16 at 07:36
  • db.users.find({ $where: "/^902.*/.users(this.mobile)" }) error: { "$err" : "TypeError: Object /^902.*/ has no method 'users' near 's.mobile)' ", "code" : 16722 } – prat_raj21 Mar 10 '16 at 07:39
  • I wish I could downvote comments. `$where` runs JavaScript code. Read up on how to write JavaScript and perform regular expression matches. – Blakes Seven Mar 10 '16 at 07:56
  • 1
    If those are phone numbers, they should not be stored as integers in the first place. – 1615903 Mar 10 '16 at 08:01
  • `db.users.find({ $where: function() { return !!('' + this.mobile).match(/^88.*/im) } });` You can use something like that – Alexandru Olaru Mar 10 '16 at 10:24
  • ok this runs perfectly db.users.find({$where:"/90.*/.test(this.mobile)"}) – prat_raj21 Mar 10 '16 at 11:21
  • Alexandru your answer also works for me – prat_raj21 Mar 10 '16 at 11:24

0 Answers0