Leo Eidinov
Leo Eidinov

Reputation: 11

Meteor: find by full name (multiple fields)

I'm implementing persons list inside my Meteor app. Every person has a lastName and firstName. I want to be able to search by full name or it's part. Of course search must be case insensitive. There will be about 500 persons, so I need pagination & I can't just filter the results on client side.

My general idea is to generate additional field fullName upon insert or update of a person:

fullName: lastName.toLowerCase() + ' ' + firstName.toLowerCase(), 

And then just use it inside the find method:

PersonsCollection.find({ fullName: {$regex : searchString}, ... })

I'm pretty new to Meteor, Mongo and backend in general. I spent some time researching, but still confused. My questions are:

Upvotes: 1

Views: 130

Answers (1)

Ricardo Aragão
Ricardo Aragão

Reputation: 386

It is usual to create an additional field.

But you can try to use an $or:

PersonsCollection.find(
  { 
    $or: [ 
      { firstName: new RegExp(searchString, 'i') },
      { lastName: new RegExp(searchString, 'i') } 
    ] 
  }
);

Don't forgot to create indexes to your search fields.

Upvotes: 0

Related Questions