MongonDb在thinkphp中常用的功能整理
1.以某字段开头的数据查询条件
$title = input('param.title');
$where['title'] = new \MongoDB\BSON\Regex("^{$title}",'i'); //已title开头的字
2.原生用法:
//query 查询列表
$command = new \MongoDB\Driver\Query($where);
$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
$lists = $manager->executeQuery("news.friend_circle", $command);
var_dump($lists->toArray());
//command用法
$command = new \MongoDB\Driver\Command($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();