在php上显示所有文档

时间:2015-08-01 22:18:06

标签: php mongodb

我想显示该系列的所有文件:我已经做过这样的文章:

<?php
    try {
           $connection = new MongoClient();
           $database   = $connection->selectDB('Myblog');
           $collection = $database->selectCollection('articles');
         } 
            catch(MongoConnectionException $e) {
            die("Failed to connect to database ".$e->getMessage());
    }

    $query=array();
    $cursor=$collection->find($query);

    foreach ($cursor as $doc) {     
        echo " Title is : ".$article['title']."";
    }
?>

但我有这个:

  

标题是:测试
  标题是:测试
  标题是:测试

这是我的收藏:

{ "_id" : ObjectId("55bd3fb50faaf28804b7acd9"), "title" : "Test1", "content" : "hello", saved_at" : ISODate("2015-08-01T21:52:53.420Z") }
{ "_id" : ObjectId("55bd3fbd0faaf2c403b7acda"), "title" : "Test2", "content" : "hey", "saved_at" : ISODate("2015-08-01T21:53:01.706Z") }
{ "_id" : ObjectId("55bd3fc90faaf2c103b7acd9"), "title" : "Test3", "content" : "hello test", "saved_at" : ISODate("2015-08-01T21:53:13.225Z") }

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

缺少什么?您只是引用title命令输出中的echo。如果你想要其他字段,你必须引用它们,即($article['content']等...)

答案 1 :(得分:0)

使用

  foreach ($cursor as $doc) {
      echo "Title is : " . $doc['title'];
  }