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();
原文地址:https://www.cnblogs.com/gaosf/p/14768315.html