Combining Collection Fields to run a search

Hey guys, I am currently building a search feature for my web app.

I have a collection “Person” with fields “FirstName” and “LastName”.

I am trying to query the collection so that when searching, if the user types the first name, it pull matching results, if they type the last name, it pulls matching results, but if they type the full name (FirstName + LastName) it singles down to a single result. I have been trying to get this with $or, $and, and a combination of the two with no luck.

I can provide more information if that would be of help.


Wouldn’t be easier to use a service like Algolia?

maybe so? But I have it working for all other search functions, just not my Person collection.

I am currently using the meteorhacks:aggregate package that allows me to use $concat from Mongo.aggregate.

Person.aggregate([ { $project: { fullName: { $concat: ["$FirstName", "$LastName"] } } };

Do you know how I can limit this list though? Right now it returns all Persons fullName. I am looking to add something like

Person.aggregate([ { $project: { fullName: { $concat: ["$FirstName", "$LastName"] } }], { fullName: "johndoe" };

but that doesn’t work for a return statement.

I ended up figuring it out.

let selector = [
   { $project: { fullName: { $concat: ["$FirstName", "$LastName"] } } },
   { $match: { fullName: regExp } },

return Person.aggregate(selector)

I wasn’t familiar with the $match aggregation (which the package mentioned previously allowed me to use), so this was a good learning experiance :slight_smile: