简体   繁体   English

无法从mongoDB获取数据

[英]Can't get data from mongoDB

I create function to get data from mongoDB by PHP code. 我创建了通过PHP代码从mongoDB获取数据的函数。 But It seem not working well. 但这似乎效果不佳。

let's see my code 让我们看看我的代码

function updateperformancescore($timepf)
    {
        $mon = new Mongo('mongodb://127.0.0.1:27017');
        $dbs = $mon->selectDB('bitdindatabase');
        $col = $dbs->selectCollection('bd_performance_score');

        $query2 = array("user_ID"=>"999");
        echo "<br>query__".$query2["user_ID"];

        $data2 = $col->find($query2,true);
        echo "<br>count___".count($data2)."<br>";
        echo "check is_object--".is_object($data2)."<br>";
        //echo "--".$data2["user_ID"]."<br>";
        error_reporting(E_ALL ^ E_NOTICE);
        foreach ($data2 as $obj) 
            {
                echo "aaaa";

                $which = array("user_ID"=>$obj["user_ID"],"time"=>$obj["ps_avgtime"],"slug"=>$obj["lesson_slug"]);

                echo json_encode($which);
            }

and webpage show only 和网页仅显示

query_ 999 count __1 check is_object--1 查询_ 999计数 __1检查is_object--1

but my data in mongo is 但是我在mongo中的数据是

{ "_id" : ObjectId("501e768eefbdb8637e2b00c8"), "user_ID" : 999, "lesson_slug" : 999, "ps_avgtime" : 999 }
{ "_id" : ObjectId("501e7698efbdb8637e2b00c9"), "user_ID" : 999, "lesson_slug" : 998, "ps_avgtime" : 888 }
{ "_id" : ObjectId("501e76a1efbdb8637e2b00ca"), "user_ID" : 999, "lesson_slug" : 997, "ps_avgtime" : 777 }
{ "_id" : ObjectId("501e76a9efbdb8637e2b00cb"), "user_ID" : 999, "lesson_slug" : 996, "ps_avgtime" : 77 }
{ "_id" : ObjectId("501e76b6efbdb8637e2b00cc"), "user_ID" : 999, "lesson_slug" : 995, "ps_avgtime" : 555 }
{ "_id" : ObjectId("501e76c0efbdb8637e2b00cd"), "user_ID" : 999, "lesson_slug" : 994, "ps_avgtime" : 444 }
{ "_id" : ObjectId("501e76caefbdb8637e2b00ce"), "user_ID" : 999, "lesson_slug" : 993, "ps_avgtime" : 333 }
{ "_id" : ObjectId("501e76d3efbdb8637e2b00cf"), "user_ID" : 999, "lesson_slug" : 992, "ps_avgtime" : 222 }
{ "_id" : ObjectId("501e76dcefbdb8637e2b00d0"), "user_ID" : 999, "lesson_slug" : 991, "ps_avgtime" : 111 }
{ "_id" : ObjectId("501e76e6efbdb8637e2b00d1"), "user_ID" : 999, "lesson_slug" : 990, "ps_avgtime" : 1 }

Why it's not going into foreach ? 为什么不进入foreach? And What's wrong with this code and How I get data correctly. 这段代码有什么问题以及如何正确获取数据。 help or guide me please :) 请帮助或指导我:)

edit 编辑

I just error_log and It show 我只是error_log并且它显示

Fatal error: Uncaught exception 'MongoException' with message 'The MongoCursor object has not been correctly initialized by its constructor' in /var/www/web/sendanswer.php:92 Stack trace: #0 /var/www/web/sendanswer.php(92): MongoCursor->rewind() #1 /var/www/web/sendanswer.php(343): sendanswer->updateperformancescore(4.4269230365753) #2 {main} Next exception 'MongoException' with message 'The MongoCursor object has not been correctly initialized by its constructor' in /var/www/web/sendanswer.php:92 Stack trace: #0 /var/www/web/sendanswer.php(92): MongoCursor->rewind() #1 /var/www/web/sendanswer.php(343): sendanswer->updateperformancescore(4.4269230365753) #2 {main} thrown in /var/www/web/sendanswer.php on line 92 致命错误:/var/www/web/sendanswer.php:92堆栈跟踪:#0 / var / www / web / sendanswer中未捕获的异常“ MongoException”,消息为“ MongoCursor对象尚未由其构造函数正确初始化”。 php(92):MongoCursor-> rewind()#1 /var/www/web/sendanswer.php(343):sendanswer-> updateperformancescore(4.4269230365753)#2 {main}下一个异常“ MongoException”,消息为“ MongoCursor对象”未在/var/www/web/sendanswer.php:92中由其构造函数正确初始化:堆栈跟踪:#0 /var/www/web/sendanswer.php(92):MongoCursor-> rewind()#1 / var / www / web / sendanswer.php(343):sendanswer-> updateperformancescore(4.4269230365753)#2 {main}放在第92行的/var/www/web/sendanswer.php中

what does it mean ?? 这是什么意思 ??

edit 编辑

That means "Probably your Mongo object is not connected to a database server." 这意味着“可能您的Mongo对象未连接到数据库服务器。”

What I see is an error in datatypes. 我看到的是数据类型错误。

In database user_ID is an integer, in your PHP query, it is a string. 在数据库中,user_ID是整数,在PHP查询中,它是一个字符串。 Try: 尝试:

$query2 = array("user_ID"=> 999);

instead of : 代替 :

$query2 = array("user_ID"=>"999");

PHP may be easy on datatypes, but I am not sure about Mongodb and its PHP driver. PHP在数据类型上可能很容易,但是我不确定Mongodb及其PHP驱动程序。

Also there is an incorrect parameter in your find statement. 您的find语句中还有一个错误的参数。 Remove the true as it should be an array of fields, not a bool 删除true因为它应该是字段数组,而不是bool

This code works correctly : 这段代码可以正常工作:

<?php

$mon = new Mongo('mongodb://127.0.0.1:27017');
$dbs = $mon->selectDB('my_db');
$col = $dbs->selectCollection('newColl');

$query2 = array("user_ID"=> 999);
echo "<br>query__".$query2["user_ID"];

$data2 = $col->find($query2);

error_reporting(E_ALL ^ E_NOTICE);
foreach ($data2 as $obj)
{

  $which = array("user_ID"=>$obj["user_ID"],
                 "time"=>$obj["ps_avgtime"],
                 "slug"=>$obj["lesson_slug"]);
  echo json_encode($which);
  echo PHP_EOL ;
}
?>

Output: 输出:

[......]$ php testmongo.php 
<br>query__999{"user_ID":999,"time":999,"slug":999}
{"user_ID":999,"time":999,"slug":999}
{"user_ID":999,"time":339,"slug":999}
{"user_ID":999,"time":339,"slug":5599}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM