-2

I am new in MongoDB and writing script in PHP and I am using MongoDB to store information. Currently, I am working on a search result and need a query to fetch result, if some characters are matched then fetch all related data.

currently, I am doing with below script

$queryString = ".*".$queryString."*";
$where = array(
    '$or' => array(
        array(
            'title' => new \MongoDB\BSON\Regex($queryString),
        ),
        array(
            'description' => new \MongoDB\BSON\Regex($queryString),
        ),
     )
);

this is working fine but still some words are skipped. Please let me know what is the best way to get exact information.

e.g:

{
    "_id" : ObjectId("5d1360536d94f726ec484426"),
    "master_id" : ObjectId("5d11bab64d51f58dbbd391bf"),
    "title" : "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).",
    "description" : "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
    "pub_date" : "\n\t   Wed, 26 Jun 2019 13:30:18 GMT\t",
    "created_on" : "2019-06-26 17:38:51",
    "modified_on" : "2019-07-10 09:34:33"
}

if we search data with character "Lor" then all if any word which have word "Lor" is found in collections(e.g Lorem),all result should be return.

this is same as LIKE query in mysql.

Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 52
akgola
  • 225
  • 2
  • 16
  • Please show us example data, a search that works and a search that doesn't work. You should also include all the relevant code, including what the query string contains and how you're actually using the variables you create here. – M. Eriksson Jul 11 '19 at 05:24
  • @MagnusEriksson,I have updated the anaswer.I need query same as like in Mysql. – akgola Jul 11 '19 at 05:52
  • I have a hard time understanding what your second last sentence mean. As I read it, it sounds like your saying it works? – M. Eriksson Jul 11 '19 at 05:58
  • It will be good before downvoting any question to tell the exact reason, as I told I am new in Mongo so maybe something is a mistake in my question.:( – akgola Jul 11 '19 at 05:59
  • The down votes are most likely not because you don't know Mongo. It's more likely to be because you still haven't included all the information we asked for (and is needed for us to be able to reproduce the issue, which we also don't have a practical example of). – M. Eriksson Jul 11 '19 at 06:02
  • @MagnusEriksson,I mean if search with string "LOR" then all related to this word like should be return after query same as in Mysql. I need same as per link https://stackoverflow.com/a/3305687/3551147 but this is not working for me – akgola Jul 11 '19 at 06:02
  • In your query, try changing `".*".$queryString."*"` to `".*".$queryString.".*"` (there's now a dot before the second `*` as well, just like in the answer you linked to). – M. Eriksson Jul 11 '19 at 06:02
  • @MagnusEriksson,still query is missing some data after applying updated code as per your suggestion. result count return 173 but it should be ~277 – akgola Jul 11 '19 at 06:07
  • you can use find() method with mongoose model and use slash like /Lor/ for search. – Kuldeep singh Jul 11 '19 at 06:16
  • Well, without a proper [minimal and reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), I can't really reproduce or test anything so... I'm out. – M. Eriksson Jul 11 '19 at 06:34

1 Answers1

1

you can go with case insensitive regex search

Try this :

$queryString = ".*".$queryString.".*";
$where = array(
    '$or' => array(
        array(
            'title' => array (
                '$regex' => new \MongoDB\BSON\Regex($queryString),
                '$options' => 'i'
            )
        ),
        array(
            'description' => array (
                '$regex' => new \MongoDB\BSON\Regex($queryString),
                '$options' => 'i'
            )
        ),
     )
);
Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 52
  • ,this gives same result,actually I have data in Hindi language so option 'i' will return the same result.May be I need add some more regular expression for better result. – akgola Jul 11 '19 at 07:10
  • maybe you are not getting the results because of the language constraints – Ravi Shankar Bharti Jul 11 '19 at 07:13
  • yes,it may be cause.thanks for helping me.Can you please help me another question https://stackoverflow.com/q/56983900/3551147 ? – akgola Jul 11 '19 at 07:36