关于php查询mongodb限制返回字段的问题
/** * 查询MongoDB* @param string 集合名
* @param array 查询的条件array<br/>如<i>array(col_a=>111)</i>
* @param array 集合过滤器array<br/>完整的样子想这个:<i>array(sort=>array(col_a=>1,col_b=>-1), skip=>100, limit=>10, timeout=>5000, immortal=>true)</i>,这表示:
* <ol>
* <li>wrapped 以wrapped为封装为数组的键,默认按数组先后顺序</li>
* <li>_id,默认不返回数据带有MongoID字段,如果指定了返回列的话也是一样的效果</li>
* <li>sort 以col_a为ASC,col_b为DESC排序,可以多列组合</li>
* <li>skip 表示从101条记录开始取数据,即跳过了前100条</li>
* <li>limit 本次选取的条数</li>
* <li>timeout 表示等待响应的时间(暂不使用)</li>
* <li>immortal 表示是否维持链接永久有效,默认true,此时timeout失效(暂不使用)</li>
* </ol>
* @param array 需要返回的字段(通常只返回必要的字段可以加快响应速度)
* @return mixed 查询的结果
*/
public function find($coll_name, $condition = array(), $result_filter = array('wrapped' => '', 'with_objectId' => 0, 'timeout' => 5000, 'immortal' => true), $ret_fields = array()) {
$db_name = $this->db_name;
$cursor = $this->mongo->$db_name->$coll_name->find($condition, $ret_fields);
if (!empty($result_filter['skip'])) {
$cursor->skip($result_filter['skip']);
}
if (!empty($result_filter['limit'])) {
$cursor->limit($result_filter['limit']);
}
if (!empty($result_filter['sort'])) {
$cursor->sort($result_filter['sort']);
}
if (!empty($result_filter['wrapped'])) {
$wrapped = $result_filter['wrapped'];
} else {
$wrapped = '';
}
if (isset($result_filter['with_objectId']) && $result_filter['with_objectId'] == 1) {
//如果指定了返回的列此项目就失效
$with_objectId = count($ret_fields) < 1;
} else {
$with_objectId = 0;
}
if(!$with_objectId){
if (isset($ret_fields['with_objectId']) && $ret_fields['with_objectId'] == 1) {
$with_objectId = 1;
} else {
$with_objectId = 0;
}
}
$result = array();
$this->cursor = $cursor;
try {
if ($wrapped == '_id') {
while ($ret = $cursor->getNext()) {
$result[$ret['_id']->{'$id'}] = $ret;
}
} else if (strlen($wrapped)) {
while ($ret = $cursor->getNext()) {
$result[$ret[$wrapped]] = $ret;
}
} else {
while ($ret = $cursor->getNext()) {
$result[] = $ret;
}
}
if (!$with_objectId) {
foreach ($result as $key => $v) {
unset($result[$key]['_id']);
}
}
} catch (Exception $ex) {
$this->errors = $ex->getMessage();
}
return $result;
}
重点在于:
页:
[1]