Summary of thinkphp Development (v) Some operations of the--MONGODB

Source: Internet
Author: User
Tags mongodb

First, the application scene: Find MongoDB news name is not "", also not NULL news name, and according to the Heat rankings:

$map [' title '] = Array (' $not ' =>array (' $in ' =>array ("", null));
$news = $this->where ($map)->order (' Value desc ')->field ("Title,class")->select ();         

If you want to get a category ranking:

$res =array ();
foreach ($news as $k) {
      Array_push ($res, $k [' title ']);
}
$rank =array_search ($this _news[' title '), $res) +1;

If you don't want order rankings, how do you rank the results of a select?

Array_multisort (Array_column ($news, ' value '), Sort_desc, $news);

Second, MongoDB fuzzy query

The Public Function Search_result ($kw) {//Fuzzy query search results
     $where [' title '] = Array (' like ', $kw);
     $results = $this->where ($where)->order ("Value desc")->select ();
     return $results;
}

Third, update the number of views in the site

 Public function Update_seen ($id) {//Update site views $arr =[$id, 0];
$querys = Array ("_id" =>array (' $in ' => $arr)); Why do I write this, not directly $querys[' _id ']= $id, because the _id of the database I'm connecting to is not the MongoDB database automatically generated, but we customize it, and when you're using $querys[' _id ',
         Thinkphp will automatically turn your $id into Objectid form, but $id itself cannot be converted to Objectid, causing an error $news = $this->where ($querys)->find ();
             if ($news) {$seen = $news [' Seen ']+0;
             if ($seen ==null or! $seen) {$seen = 0;
             $data [' Seen ']= $seen +1;
             $querys = Array ("_id" =>array (' $in ' => $arr));
         $this->where ($querys)->save ($data); }
     }
Related Article

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.