Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5838181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:26:26+00:00 2026-05-22T11:26:26+00:00

I want to search for a partial first and last name match – for

  • 0

I want to search for a partial first and last name match – for example in sql

f_name LIKE J%   OR  l_name LIKE S%   

would match John Smith or John Henry or Harry Smith .

I am assuming I may need to use the “$or” operator,

I have this so far that I believe is doing the LIKE % part properly, but I believe it is doing an “AND” search (meaning it searches for f_name LIKE J% AND l_name LIKE S% so it would only match John Smith):

$name1="J";
$name2="S";
$cursor = $this->clientCollection->find(array('f_name' => new MongoRegex('/^'.$name1.'/i'), 'l_name' => new MongoRegex('/^'.$name2.'/i') ));

Note: This will match containing as in %J%

MongoRegex('/'.$name1.'/i')

This will match starts with (notice the added ^) as in J%

MongoRegex('/^'.$name1.'/i')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:26:27+00:00Added an answer on May 22, 2026 at 11:26 am

    $or takes an array of clauses, so you basically just need to wrap another array around your current query:

    array('$or' => array(
        array('f_name' => new MongoRegex('/'.$name1.'/i')), 
        array('l_name' => new MongoRegex('/'.$name2.'/i'))
    ));
    

    Edit: the previous example missed an inner set of array() calls.

    The original, wrong, example that I posted looked like this:

    array('$or' => array(
        'f_name' => new MongoRegex('/'.$name1.'/i'), 
        'l_name' => new MongoRegex('/'.$name2.'/i')
    ));
    

    This is a valid query, but not a useful one. Essentially, the f_name and l_name query parts are still ANDed together, so the $or part is useless (it’s only getting passed one query, so it’s the same as just running that query by itself).

    As for the alternative you mentioned in your comment, that one doesn’t work because the outermost array in a query has to be an associative array. The confusion arises because Mongo’s query syntax is JSON-like and uses a mixture of objects and arrays, but both of those structures are represented in PHP by arrays. The PHP Mongo driver basically converts PHP associative arrays to JSON objects ({ ... }), and “normal” PHP arrays to JSON arrays ([ ... ]).

    The practical upshot is that “normal” PHP arrays are generally only valid when inside an associative array, like when specifying multiple values for a field. The following example from the PHP Mongo manual shows a valid usage of a “normal” array in a query:

    $cursor = $collection->find(array("awards" => array('$in' => array("gold", "copper"))));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like a regular expression to match given a partial or camel cased
I would like to be able to use Full text search in Code First
I want to search key words (keys would be dynamic) and replace them in
I want to search two string in datarows.For example; string1=ex string2=ex2 row1={'ex','ex2','ex3'....}---->True row2={'ex3','ex1','ex2'....}---->True row3={'ex2','ex5','ex6'....}---->False
I want to create a very simple search partial. It has a text box,
I see an example of doing a partial string search on the GAE google
I want to have two partial views , one for SEARCH and one for
I need to search for a partial match in an inverted index, following code
I want to search the program >> that is by which you append to
I want to search each line for the word FAILED , then print the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.