简体   繁体   English

MongoDB中的PHP查询不起作用

[英]PHP Query in MongoDB doesn't work

I have been trying to fix this problem since days and I can't really solve it. 几天以来,我一直在尝试解决此问题,但我无法真正解决。

I'm trying to use mongo DB for my first time and here's my problem: 我正在尝试第一次使用mongo DB,这是我的问题:

$id = utf8_encode($_POST['mongo']);
$query=array("id" => $id);
$conn = new Mongo("mongodb://localhost:27017");
$database = $conn->test;
$collection = $database->pages;
$doc = $collection->findOne($query);

The $id variable is set to 2, but findOne doesn't return anything. $id变量设置为2,但是findOne不返回任何内容。

If I try for example to change the id value in array with 2 [$query=array("id" => 2);] the DB returns the document that I need. 例如,如果我尝试使用2 [$query=array("id" => 2);]更改数组中的id值,则DB返回我需要的文档。
It's a mystery ahah. 这是一个谜啊。
Can anyone see an error? 谁能看到错误?
Thanks 谢谢
L 大号

After connect you should be select your document 连接后,应选择您的文档

$id = utf8_encode($_POST['mongo']);
$query=array("id" => $id);
$m = new MongoClient('mongodb://localhost:27017');
$db = $m->selectDB('yourdocumentname');
$collection = new MongoCollection($db, 'yourcollectionname');


$doc = $collection->findOne($query);
    var_dump($doc);

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

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