我需要检查PHP7中的MongoDB查询中是否存在字符串。过去很容易这样:
// Trying to find $username
$dbserver = new MongoClient();
$db = $dbserver->databasename;
$collection = $db->collectionname;
$queryDbFind = $collection->findOne(array("user" => $username));
if(empty($queryDbFind[$username])){
echo "empty";
} else {
echo "not empty";
};
现在有了新的PHP驱动程序,我按如下方式执行查询:
$db = new MongoDB\Driver\Manager("mongodb://localhost:27017")
$query1 = new MongoDB\Driver\Query(['user' => $username], ['limit' => 1, 'projection' => ['user' => 1, '_id' => 0]]);
$dbread1 = $db->executeQuery('goodcv.userbase', $query1);
这是我被困的地方。我得到了该对象,但无法检查该对象中是否存在$ username。
我试过了:
$result1 = $dbread1->toArray();
if(empty($result1->user)){
echo "user exist";
} else {
echo "user does not exist";
};
不起作用:( 返回结果的唯一方法是:
foreach($result1 as $row){
echo $row->user;
};
但这不是正确的做法吗?