PHP code example of salamtam / mongo-query-php
1. Go to this page and download the library: Download salamtam/mongo-query-php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
salamtam / mongo-query-php example snippets
use MongoQueryPHP\MongoQueryPHP;
/* set config */
$config = [
'url' => 'mongodb://localhost:27017',
'database' => 'test',
'collection' => 'member'
];
/* set query (filter) */
$filter = [
'name' => 'salamtam'
];
/* connect database */
$db = new MongoQueryPHP($config);
$db->setDatabase($config['database'])->setCollection($config['collection']);
/* query database */
$cursor = $db->query($filter);
/* result */
foreach ($cursor as $doc) {
echo json_encode($doc) . PHP_EOL;
break;
}
use MongoQueryPHP\MongoQueryPHP;
/* set config */
$config = [
'host' => 'localhost',
'port' => '27017',
'user' => 'root',
'pass' => '',
'database' => 'test',
'collection' => 'member'
];
/* set query (filter) */
$filter = [
'name' => 'salamtam'
];
/* connect database */
$db = new MongoQueryPHP($config);
$db->setDatabase($config['database'])->setCollection($config['collection']);
/* query database */
$cursor = $db->query($filter);
/* result */
foreach ($cursor as $doc) {
echo json_encode($doc) . PHP_EOL;
break;
}
use MongoQueryPHP\MongoQueryPHP;
/* set config */
$config = [
'url' => 'mongodb://localhost:27017',
'database' => 'test',
'collection' => 'member'
];
/* set query (filter) */
$filter = json_decode('{"name":"salamtam","created_at":{"$gte":1499533200000}}', true);
/* connect database */
$db = new MongoQueryPHP($config);
$db->setDatabase($config['database'])->setCollection($config['collection']);
/* query database */
$cursor = $db->query($filter);
/* result */
foreach ($cursor as $doc) {
echo json_encode($doc) . PHP_EOL;
break;
}
use MongoQueryPHP\MongoQueryPHP;
/* set config */
$config = [
'url' => 'mongodb://localhost:27017',
'database' => 'test',
'collection' => 'member'
];
/* set query (filter) */
$filter = [
'name' => 'salamtam',
'created_at' => [
'$gte' => 1499533200000,
],
];
$sort = [
'sort' => [
'created_at' => -1
],
];
/* connect database */
$db = new MongoQueryPHP($config);
$db->setDatabase($config['database'])->setCollection($config['collection']);
/* query database */
$cursor = $db->query($filter, $sort);
/* result */
foreach ($cursor as $doc) {
echo json_encode($doc) . PHP_EOL;
break;
}
use MongoQueryPHP\MongoQueryPHP;
/* set config */
$config = [
'url' => 'mongodb://localhost:27017',
'database' => 'test',
'collection' => 'member'
];
/* set query (filter) */
$filter = [
'$match' => [
'name' => 'salamtam'
]
];
/* connect database */
$db = new MongoQueryPHP($config);
$db->setDatabase($config['database'])->setCollection($config['collection']);
/* aggregate database */
$cursor = $db->aggregate($filter);
/* result */
foreach ($cursor as $doc) {
echo json_encode($doc) . PHP_EOL;
break;
}
use MongoQueryPHP\MongoQueryPHP;
/* set config */
$config = [
'url' => 'mongodb://localhost:27017',
'database' => 'test',
'collection' => 'member'
];
/* set query (filter) */
$filter = json_decode('[{"$match":{"name":"salamtam","created_at":{"$gte":1499533200000}}}]', true);
/* connect database */
$db = new MongoQueryPHP($config);
$db->setDatabase($config['database'])->setCollection($config['collection']);
/* aggregate database */
$cursor = $db->aggregate($filter);
/* result */
foreach ($cursor as $doc) {
echo json_encode($doc) . PHP_EOL;
break;
}