Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: $regex requires regular expression #125

Closed
jeremytenjo opened this issue Apr 3, 2018 · 8 comments
Closed

Error: $regex requires regular expression #125

jeremytenjo opened this issue Apr 3, 2018 · 8 comments

Comments

@jeremytenjo
Copy link

const searchByName = async (data) => {
  let db = await MongoConnect(),
    collection_users = db.collection('users'),
    users

  console.log(data.name)

  let regxName = new RegExp(data.name)

  console.log(regxName)

  try {
    let users = await collection_users.find({ name: { $regex: regxName, $options: 'i' } }).execute()
    console.log(users)
  } catch (error) {
    return error
  }

  return users
}
@edaniels
Copy link
Contributor

edaniels commented Apr 3, 2018

@tenjojeremy, the $regex in your code is actually a relaxed extended JSON regular expression. Can you try passing { name: bsonRegex } where bsonRegex is BSON.BSONRegExp(pattern)?

You can get the BSON package from import { BSON } from 'mongodb-stitch';

@jeremytenjo
Copy link
Author

hi @edaniels, I tried it like this:

import { BSON } from 'mongodb-stitch'

const searchByName = async (data) => {
  let db = await MongoConnect(),
    collection_users = db.collection('users'),
    users

  console.log(data.name)

  let regxName = new RegExp(data.name)

  console.log(regxName)

  let bsonRegex = BSON.BSONRegExp(regxName, 'i')

  console.log(bsonRegex)

  try {
    users = await collection_users.find({ name: bsonRegex }).execute()
    console.log(users)
    return users
  } catch (error) {
    return error
  }
}

I dont get an error now, but instead I am getting all the users even if they dont match the pattern.

@edaniels
Copy link
Contributor

edaniels commented Apr 3, 2018

What's the pattern? Have you tried the same pattern in the mongo shell?

@jeremytenjo
Copy link
Author

My pattern is /hay/ and it works in the mongo shell and it still doesent work

let bsonRegex = BSON.BSONRegExp('hay', 'i')

I get this when I console.log(bsonRegex)

BSONRegExp {_bsontype: "BSONRegExp", pattern: "", options: ""}

@edaniels
Copy link
Contributor

edaniels commented Apr 4, 2018

Try let bsonRegex = new BSON.BSONRegExp('hay', 'i') instead. Pretty sure you hit this bug: mongodb/js-bson#242

@jeremytenjo
Copy link
Author

@edaniels Yes! that worked.

Thank you.

@edaniels
Copy link
Contributor

edaniels commented Apr 4, 2018

Woo! We'll publish a new SDK with that fix once the change goes in on the BSON library. Have fun developing!

@edaniels edaniels closed this as completed Apr 4, 2018
@richtera
Copy link

richtera commented Apr 23, 2019

I am running into this again. It seems to match none of my records with my regex even though I am passing BSON.BSONRegExp() but I am using the server-sdk. Works fine inside of mongo-shell and when I serialize the whole query using JSON.stringify() it does look correct:
{ info: new BSON.BSONRegExp("^all:") } serializes to { "info": { "pattern": "^all:", "options": "" } } but seems to cause no records being returned. When I do the same query in the shell I get
db.my_collection.find({info: {$regex: "^all:" }}).count() 2320 records. I am not quite sure at what level this fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants