从mongodb集合中获取所有数据 [英] fetch all data from mongodb collection

查看:1460
本文介绍了从mongodb集合中获取所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码从mongodb中的集合中获取所有数据

I am trying to fetch all data from my collection in mongodb using the following code

public function fetch_employee_list()
        {

            $m = new MongoClient();
            $db = $m->selectDB('fleet');
            $collection = new MongoCollection($db, 'employee');
            // $name = array('Type' => 'name');
            $cursor = $collection->find();
            echo "<pre>";
            print_r($cursor);
            exit;
         }

我得到的结果是
MongoCursor Object()
,但是当我使用

and the result I am getting is "MongoCursor Object()" but when i use

$cursor = $collection->findOne();

它给了我一个数组结果。
我做错了什么?

it gives me one result as array. what I did wrong?

推荐答案

您需要执行

$cursor = $collection->find();
foreach ($cursor as $obj) {
    echo $obj . "\n";
}

这篇关于从mongodb集合中获取所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆