zoukankan      html  css  js  c++  java
  • MongonDb在thinkphp中常用的功能整理(模糊查询)

    1.以某字段开头的数据查询条件

    $title = input('param.title');
    $where['title'] = new MongoDBBSONRegex("^{$title}",'i');  //已title开头的字
    //tp3.2.3亲测
    if (!empty($params['Domain'])) {
    $where['domain_name'] = ['regex', '/.*'.$params['Domain'].'.*/'];
    }

    $pipeline = [
    [
    '$match' =>
    [
    'domain_status' => ['$ne' => 0],
    'user_id' => ['$in' => $where['user_id'][1]]
    ]
    ],
    ['$sort' => ['domain_id' => -1]]
    ];
    $result = (new DomainInfoService())->getAggregateList($pipeline);
    public function getAggregateList($pipeline, $options = []){
    return D($this->model)->aggregate($pipeline,$options);
    }

    2.原生用法:

    //query  查询列表

    $command = new MongoDBDriverQuery($where);
    $manager = new MongoDBDriverManager('mongodb://localhost:27017');
    $lists = $manager->executeQuery("news.friend_circle", $command);
    var_dump($lists->toArray());

    //command用法
    $command = new MongoDBDriverCommand($document);
    var_dump($command);
    $list = $mongoModel->Command($command,"friend_circle");
    var_dump($list);

    3.根据某字段模糊查询

    1)命令行:db.news_live.find({"title":/测试?/i});

    2)代码查询
    $query=array("name"=>new MongoRegex("/.*”.$name.".*/i"));
    3.根据数据库某字段排序(比如根据时间排序)
    $order = array('create_time'=>-1); //-1表示降序,1表示升序
    $list = $mongo->order($order)->select();
  • 相关阅读:
    python常用字符串处理(转)
    Python读写文件基础.py
    Python异常和调试.md
    Python类的进阶.md
    Python模块和类.md
    python函数基础:调用内置函数&定义函数
    Spring.Net学习笔记(二)-数据访问器
    Spring.Net学习笔记(1)-容器的使用
    Spring.Net学习笔记(0)-错误汇总
    VS中设置xml智能提示
  • 原文地址:https://www.cnblogs.com/gaosf/p/14768315.html
Copyright © 2011-2022 走看看