【问题标题】:Connect my mongo with PHP(filter returned document)用 PHP 连接我的 mongodb(过滤返回的文档)
【发布时间】:2015-02-02 08:43:39
【问题描述】:

我正在尝试使用 php 代码连接到我的 mongo 数据库。我不太确定我已经尝试过什么。这是我的代码:

$mongoHostname = ".. . ... . ... . ...";//"localhost";
$databaseName = "db";

$conn = new Mongo('mongodb://'.$mongoHostname.'/'.$databaseName);
// access database
$db = $conn->$databaseName;
// access collection
$collection = $db->coll;

$file = 'names.txt'; //a file with screenames
$data = file($file) or die('Could not read file!');

foreach ($data as $line)
{
  $cursor = $collection->find(array('user.screen_name' => $line));
  print_r($cursor);

}

我在 mongo 中的集合 coll 有一个字段用户,其中包含关于用户的几个元素,其中一个是 screen_name。如何显示我的查询对象?我是否正确连接了我的 mongo 数据库?我怎样才能检查它?我已经安装了php驱动程序。上面的代码在没有回显的情况下工作。

编辑:基本上我发现我必须使用以下命令ini_set('mongo.long_as_object', 1); 我不知道这个命令到底代表什么。最后我设法返回包含所有用户数据的游标对象。如何过滤该文档以提取特定字段,例如 user.description 和 user.location?我试图做这样的事情:

foreach ($data as $line)
{
 $line = preg_replace('/\s+/', '', $line);
 $cursor = $collection->find(array('user.screen_name' => $line));

 while ($cursor->hasNext()){

    $nextCursor = $cursor->getNext(); 
    $text = (string)$nextCursor["text"];
    echo $text. '<br>';
    //process next cursor
 }

    $descr = (string)$cursor["user"]['description'];
    $location = (string)$cursor["user"]['location'];
    $url = (string)$cursor["user"]['url'];
    $image_url = (string)$cursor["user"]['profile_image_url'];

    echo "user information:   ".$descr.'<br>';
    echo 'user location:    '.$location.'<br>';
    echo "url: ".$url."<br>";
         echo  "<a href=\"" . $temp . "\" style=\"text-decoration:none;\">" . $temp . "</a><br>\n";
 echo "<br><br>";
 }

虽然第一个 while 循环可以很好地打印文档文本,但在 while 循环之外我在打印其余字段时遇到了问题。

【问题讨论】:

    标签: php mongodb


    【解决方案1】:

    如何过滤特定字段:

    $fields = array('user.description'=>true);
    $cursor = $collection->find(array('user.screen_name' => $line),$fields);
    

    将结果用作数组:

    $array = iterator_to_array($cursor);
    

    请阅读官方文档: http://php.net/manual/en/mongocollection.find.php

    【讨论】:

    • 如果我使用您的方法,如何使用 $array 变量访问描述。当我 var_nump($array) 得到过滤后的文档时,但是当我尝试执行类似 $array['user']['description'] 之类的操作时出现错误。
    【解决方案2】:

    最后,我所做的是迭代每个文档,而不是所有文档。我的代码:

    $temp_url = "https://twitter.com/";
    foreach ($data as $line)
    {
    
     $line = preg_replace('/\s+/', '', $line);
     $cursor = $collection->find(array('user.screen_name' => $line));
    
     while ($cursor->hasNext()){
    
        $nextCursor = $cursor->getNext(); 
        $text = (string)$nextCursor["text"];
        echo $text. '<br>';
    
        $img = $nextCursor["images"];
        foreach ($img as $image) {
            print_r($image['link']);
            echo "<img src='" . $image['link'] . "' alt='error'>";
            echo '<br>';
        }
    
        //process next cursor
          foreach($cursor as $document) {  
             echo ($document['user']['description'].'<br>');
             echo ($document['user']['location'].'<br>');
             echo ($document['user']['url'].'<br>');
        }  
     }
    

    【讨论】:

      猜你喜欢
      • 2014-09-11
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 2015-07-23
      相关资源
      最近更新 更多