How to find a partial match based on a number in mongodb?

I have a document that has some IDs (not objectID) that are numbers. There are also times when no parentId will be passed to the function. Currently I have

return ExampleModel.find({
  $or: [
      parentId: new RegExp(exampleDtoSearch.parentId.toString()),
    { /*other things to search*/ } 

The error I am getting if I hand in a parentId is " AssertionError: The expression evaluated to a falsy value. "

And the error I get if there is no id is that it can't do toString on undefined.

How do I do a regex (or something similar) to do partial matches for numbers in mongodb/mongoose?

Read more here:

Content Attribution

This content was originally published by Morgan Smith at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: