Detailed PHP7 how to implement MongoDB fuzzy query

Source: Internet
Author: User
Php7 How to implement MongoDB fuzzy query? MongoDB Fuzzy query statement believe that everyone is not unfamiliar, this article mainly introduces in PHP 7 MongoDB implementation of Fuzzy Query method, the text gives a detailed introduction and sample code, for everyone has a certain reference learning value. We hope to help you.

Objective

In the actual development, there are a lot of scenes need to use to fuzzy query, MongoDB Shell fuzzy query is very simple:

Db.collection.find ({' _id ':/^5101/})

The above sentence is the query _id start with ' 5101 ' content.

In the old Mogodb fuzzy query is quite simple, here simple record under the fuzzy query operation mode:

Under command line:

Db.letv_logs.find ({"CTime":/uname?/i});

PHP operations

$query =array ("name" =>new Mongoregex ("/.*". $name. ". */i ")); $db->find ($query);

The following is mainly about the new PHP driver how to query:

$query = new \mongodb\driver\query (' _id ' = [' $regex ' = ' ^5101 ']); $this->getmanager ()->executequery ($ This->dbname. $this->collection, $query);

Above is the new driver in the implementation of fuzzy query, to tell the truth, quite spit groove This new driver, compared to the old driver, the function name is too long ... Faster than Swift's function name. And many of the features on the old drive have been killed on the new drive. Although a class library is provided for mongodb php library operation, there are more than 60 files in this library, sometimes more than my project files, which is what. This I suggest to encapsulate a driver class to use.

Above the spit of the groove is a little off-topic, in addition to direct fuzzy query, in and $in or $nin use, the need to pay special attention to:

$filter = [' _id ' = [' $in ' and ' = ' $regex ' = ' + ' ^5101 ']];

If you write filter as above, a fatal error will be thrown when executing:

PHP Fatal error:uncaught mongodb\driver\exception\connectionexception: $in needs an array in filename

Here $in we need to provide an array, so we change the above $filter and give it a number of groups past:

$filter = [' _id ' = [' $in ' and ' = ' [' $regex ' + = ' ^5101 ']];

Unfortunately, it is not possible to succeed in getting the results you want:

PHP Fatal Error:uncaught mongodb\driver\exception\connectionexception:cannot Nest $ under $in in filename

There $in is no such thing as a $ in this case. In fact $in $nin , to use fuzzy matching in or, you need to use \MongoDB\BSON\Regex an instance of the class:

$filter = [' _id ' = [' $in ' = = [New \mongodb\bson\regex (' ^5101 ', ' I ')]];

This time we finally got the results we wanted.

Related recommendations:

PHP Implementation MongoDB Custom build self-increment ID

MongoDB View Execution Plan

How to use the MongoDB index

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.