0

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"}, ? ]})
2
  • This is a case where writing pseudocode would make the question a lot more clear. So let's say the user types "son" - you want that to match users with, for example, the first name or "Wilson", or the last name of "Robinson"? I'm confused about the plumbers requirement - do you only want to search for plumbers or you want a result set first? Commented Oct 19, 2015 at 12:42
  • @DavidWeldon the type of the user is plumber and from them I have to get those elements which has son in their name. or we can say it like this too that we get the user whose name has son in them like Robinson or Wilson and then from that result we get those who are plumbers Commented Oct 19, 2015 at 13:11

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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'

4 Comments

can you give me an example
check the mongodb documentation. Search for aggregation pipeline and $match.
there is one thing I would like to know that is there a way to check whether a string contains the value of the field ??
I m not sure but check out $substr and $regex.
0

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")}}]}]});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.