简体   繁体   English

在 mongoDB $collection->find() 中不起作用

[英]In mongoDB $collection->find() doesn't works

Here is a code i'm trying to making work, i'll use it for an authentification page on my website:这是我正在尝试制作的代码,我将其用于我网站上的身份验证页面:

<?php

 $m = new MongoClient();

 $db = $m->myDB;

 $collection = $db->users;

 $cursor = $collection->find({"Login":"admin","Password":"password"});

 echo "No Error";

?>

I already created and filled the collection "users", and i get a result when i put this in the terminal:我已经创建并填充了“用户”集合,当我将它放入终端时,我得到了一个结果:

db.users.find({"Login":"admin","Password":"password"})

But i doesn't work in the script, ie the page doesn't echo "No Error"但我不在脚本中工作,即页面不回显“无错误”

Thanks谢谢

ps:附:

I inserted data this way in my collection:我以这种方式在我的集合中插入了数据:

$m = new MongoClient();

$db = $m->myDB;

$collection = $db->users;

$document = array( 
  "Login" => "admin", 
  "Password" => "password"
);
$collection->insert($document);
<?php

// MongoClient
$m = new MongoClient();

// Select your database
$db = $m->selectDB('database_name');

// MongoCollection, pasing $db and collection name
$collection = new MongoCollection($db, 'users');

// Your query
$query = array(
    'Login' => 'admin',
    'Password' => 'password'
);

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

You can find more at http://php.net/manual/en/mongocollection.find.php您可以在http://php.net/manual/en/mongocollection.find.php找到更多信息

You can retrieve your data from collection in this way also-您也可以通过这种方式从集合中检索数据-

   // connect to mongodb
   $m = new MongoClient();
   echo "Connection to database successfully" . "<br/>";

   // select a database
   $db = $m->mydb;
   echo "Database mydb selected" . "<br/>";

   // select a collection
   $collection =  $db->users;
   echo "Collection selected succsessfully" . "<br/>";

   // find all informations
   $cursor = $collection->find();

   // iterate cursor to display Login
   foreach ($cursor as $info) {
      echo $info["Login"] . "\n";
   }

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

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