My goal is to locate my users in the database by searching for both their first and last names, and then retrieving the complete object of each user. However, my current code only returns the concatenated `firstName` and `lastName` as `name` along with the `_id`.
Here is the code I am working with:
results = await StageOne.aggregate([
{ $project: { "name": { $concat: ["$firstName", " ", "$lastName"] } } },
{ $match: { "name": { $regex: searchInput, $options: 'i' } } }
]).collation(
{ locale: 'en', strength: 2 }
).limit(limit).skip(offset);
After executing the code, the response I receive is in the format:
{ _id: 5f064921a8900b73174f76a1, name: 'John Doe' }
What I actually want is the complete user object like this:
{ _id: 5f08fc3b8f2719096146f767, firstName: 'John', lastName: 'Doe', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="39535651575d565c">[email protected]</a>' ... createdAt: 2020-07-10T23:39:39.310Z, updatedAt: 2020-07-10T23:39:39.310Z, __v: 0 }
To achieve this, I would need to execute separate queries for `firstName` and `lastName` like this:
results = await StageOne.find({ firstName: { $regex: searchInput, $options: 'i' } }).collation(
{ locale: 'en', strength: 2 }
).limit(limit).skip(offset);