如何使用“ MongoDB \ Driver \ Query($ filter,$ options)”?

时间:2019-06-13 12:28:46

标签: php mongodb php-7.1 php-mongodb

我试图使用MongoDB \ Driver \ Query类在PHP中进行查询,但是使用php.net中的文档却并不真正了解如何使其工作。 我想返回一个JSON对象,其中包含集合中的所有数据。

对于运行PHP 7.1.2和MongoDB 3.2.20的功能PHP

$m = new MongoDB\Driver\Manager("mongodb://login:password@127.0.0.1:27017/");

$filter = array('id' => 0);
$options = array(
    'projection' => ['name' => $parameters['baseName']]
);

$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $m->executeQuery(''db_name.my_collection', $query);

$myJson = json_decode(json_encode($cursor),true);

2 个答案:

答案 0 :(得分:0)

您可以按照here的说明使用它:

"DBConnection": {
    "value": "[concat('Server=tcp:', concat(resourceId('Microsoft.Sql/servers', parameters('sqlServerName'))).fullyQualifiedDomainName, ',1433;Initial Catalog=', parameters('sqlDBName'), ';User Id=', parameters('sqlAdminLogin'), '@', parameters('sqlServerName'), ';Password=', parameters('sqlAdminPassword'), ';Trusted_Connection=False;Encrypt=True;Connection Timeout=30;MultipleActiveResultSets=True')]"


"DBConnection": {
    "value": "[concat('Server=tcp:', resourceId('Microsoft.Sql/servers', parameters('sqlServerName'))).fullyQualifiedDomainName, ',1433;Initial Catalog=', parameters('sqlDBName'), ';User Id=', parameters('sqlAdminLogin'), '@', parameters('sqlServerName'), ';Password=', parameters('sqlAdminPassword'), ';Trusted_Connection=False;Encrypt=True;Connection Timeout=30;MultipleActiveResultSets=True')]",

"DBConnection": {
    "value": "[concat('Server=tcp:', parameters('sqlServerName').fullyQualifiedDomainName,',1433;Initial Catalog=', parameters('sqlDBName'), ';User Id=', parameters('sqlAdminLogin'), '@', parameters('sqlServerName'), ';Password=', parameters('sqlAdminPassword'), ';Trusted_Connection=False;Encrypt=True;Connection Timeout=30;MultipleActiveResultSets=True')]"

答案 1 :(得分:0)

try
    {
        $client = new MongoDB\Driver\Manager("mongodb://login:password@127.0.0.1:27017/");
    }
    catch (Exception $e)
    {
        echo "***DB Client not created*** " . $e->getMessage();
        saveEvent("DB Client not created" . $e->getMessage(), $owner);
        exit();
    }
    $filter = ['id'=> $lookForID];
    $query = new MongoDB\Driver\Query($filter);
    try
    {
        $rows = $client->executeQuery('dbName.collectionName', $query);
    }
catch (Exception $e)
    {
        echo "***search failed*** " . $e->getMessage();
        exit();
    }
    foreach($rows as $r){
       print_r($r);
    }