Skip to content

Instantly share code, notes, and snippets.

@matiaslopezd
Last active February 14, 2020 05:05
Show Gist options
  • Save matiaslopezd/158aeab5f43f998193807ec3bd78d68b to your computer and use it in GitHub Desktop.
Save matiaslopezd/158aeab5f43f998193807ec3bd78d68b to your computer and use it in GitHub Desktop.
How to use $regex of MongoDB with FeathersJS

How to use

First before to use $regex in query params, need understand the nomenclature of nested query params format in feathersJS.

The [String|Number] represent a object value when the value inside of array is String. Meanwhile that value inside of array is a Number represent an array.

For example, for set a object like a value of name key need write like this: name[key]=value, this is equal to:

{
  name: {
    key: value,
  },
}

Examples

Ex. (case-sensitive)

?name[$regex]=John

{
  name: {
    '$regex': 'John',
  }
}

Ex. (case-insensitive)

?name[$regex]=joh&name[$options]=i

{
  name: {
    '$regex': 'joh',
    '$options': 'i'
  }
}

Ex. (case-insensitive exact match)

?name[$regex]=^john$&name[$options]=i

{
  name: {
    '$regex': '^john$',
    '$options': 'i'
  }
}

Ex. (multiples case-insensitive)

?$or[0][name][$regex]=joh&$or[0][name][$options]=i&$or[1][email][$regex]=@gmail&$or[1][email][$options]=i

{
  name: {
    '$regex': 'joh',
    '$options': 'i',
  },
  email: {
    '$regex': '@gmail',
    '$options': 'i',
  },
}
@matiaslopezd
Copy link
Author

matiaslopezd commented Feb 14, 2020

Remember that in order to query in URL params like the examples need use FeathersJS!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment