Reputation: 2842
Hello guys I wanted to make a nested query but I can't figure out how to do it. I want to see if the last name or first name occurs in the search result and then to see the user returned from it are plumbers. I am stuck on how to make a nested query in which I can see if the first name is a substring of the searched name or last name is the substring of the searched name. Any help would be appreciated. I have googled but I can't find a solution to this problem
Meteor.user.find({$and:[{"profile.type": "plumber"}, ? ]})
Upvotes: 0
Views: 423
Reputation: 2842
thanks guys for all the help but I kind of did it this way
t= Session.get("plumberSearchQuery").split(" ");
regexQueryString = ".*" +t[0];
for(var i = 1 ; i < t.length ; i++){
if (t[i]!= ""){
regexQueryString= regexQueryString+ "|"
regexQueryString =regexQueryString+ t[i];
}
}
regexQueryString = regexQueryString + "*."
return results.find({ $and:[{"profile.type": "plumber"},{$or: [{"profile.firstName": {$regex: new RegExp(regexQueryString, "i")}} , {"profile.lastName": {$regex: new RegExp(regexQueryString, "i")}}]}]});
Upvotes: 0
Reputation: 379
Aggregation might be nicer, but this should work:
db.user.find({"profile.type": "plumber", $or:[{"profile.name":/james/},{"profile.surname":/james/}]})
So you have $or for name and surname, and all is in one big document with plumber. /james/ is regex, keep in mind you will have to handle uppercase characters somehow.
Upvotes: 1
Reputation: 64312
Give something like this a try:
// search parameters
var search = 'son';
var type = 'plumber';
// regular expression based on the search with case ignored
var re = new RegExp(search, 'i');
// returns (type == 'plumber') AND
// (last contains 'son' OR first contains 'son')
var selector = {
$or: [{'profile.firstName': re}, {'profile.lastName': re}],
'profile.type': type
};
// fetch the users
var users = Meteor.users.find(selector).fetch();
You may need to change some of the field names, e.g. I don't know if you use profile.firstName
vs profile.firstname
, etc.
Upvotes: 1
Reputation: 1406
I think you should use aggregation pipeline for this. You could pipe the output of a $match which checks for first name or last name and then pipe its result to 2nd $match which filters for 'plumbers'
Upvotes: 0