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
.