1

I have a scenario where peoples names like John J. Doe and John J Doe exist in two seperate databases

Using values from the first database I need to query my second database for an either or scenario and still return a match

[
  { _id: ..., name: 'John J Doe' },
  { _id: ..., name: 'John J. Doe' },
  { _id: ..., name: 'John J Doe' }
]

What can I do to query and get all results?

currently I am doing this:

const name = 'John J Doe'
records = db.getCollection('job_status_containers').find({ name: /^${name}/i })

an $or query is not an option, this is going to be coming in from an API request and it just needs to be able to find it without much fluff

GET /some_endpoint?name=${encodeURIComponent(name)}
alilland
  • 2,039
  • 1
  • 21
  • 42
  • 1
    `^John J\.? Doe$` - This might help.Refer to demo link https://regex101.com/r/3dhkVc/1 . – novice Jul 12 '22 at 04:47
  • It’ll be more efficient if you can add a field normalising the names so you can do a simple search on that instead, if it’s an option. Not sure if still relevant today, but [here’s a reference](https://stackoverflow.com/a/14535022/761202). – AD7six Jul 12 '22 at 08:22

1 Answers1

1

Hacky, but I did the following and it functionally works

docs with a .

const name = 'John J. Doe'
const records = db.getCollection('job_status_containers').find({ name: /^${name}/i })

console.log(records.length)
// 159

docs without a .

const name = 'John J Doe'
const records = db.getCollection('job_status_containers').find({ name: /^${name}/i })

console.log(records.length)
// 72

querying for both

  • get rid of all .
  • replace all spaces with a regex .? that makes the . optional, and since the . can appear in unpredictable places in peoples names - just add after every word
const name = 'John J. Doe'.replaceAll('.', '').replaceAll(' ', '[.]? ') + '[.]?'
const records = db.getCollection('job_status_containers').find({ name: /^${name}/i })

console.log(records.length)
// 231
alilland
  • 2,039
  • 1
  • 21
  • 42