1. Go to this page and download the library: Download bnomei/kirby-mongodb 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/ */
bnomei / kirby-mongodb example snippets
class DefaultPage extends \Kirby\Cms\Page
{
use \Bnomei\ModelWithKhulan;
}
// using the collection
$document = khulan()->find(['uuid' => 'XXX']);
// find a single page by uuid string
$page = khulan((string) $myIdOrUUID);
// find a single page by uuid by field-key
$page = khulan(['uuid' => 'page://betterharder']);
// find all pages with a template
$pages = khulan(['template' => 'post']);
// find a user by email or Id
$user = khulan($emailOrId);
// find a file by filename and template
$file = khulan(['filename' => 'my-image.jpg', 'template' => 'image']);
// find all pages that have another page linked
$pages = khulan([
'related[]' => ['$in' => ['page://fasterstronger']],
]);
$pages = khulan([
'related{}' => ['$in' => ['dc9f7835c2400cc4']], // objectId
]);
// find all products in the category 'books' or 'movies'
// that had been modified within the last 7 days
$pages = khulan([
'template' => 'product',
'category[,]' => ['$in' => ['books', 'movies']],
'modified' => ['$gt' => time() - (7 * 24 * 60 * 60)]
]);
/** @var \MongoDB\Driver\Cursor $films */
$films = khulan()->aggregate([
[
// only get pages with template 'film'
'$match' => ['template' => 'film'],
],
[
// lookup actors by their mongodb objectId
// this will create an array of objects
'$lookup' => [
'from' => 'kirby',
'localField' => 'actors{}',
'foreignField' => '_id',
'as' => 'actor_details',
],
],
[
// only get the fields we need
// to make the query even faster
'$project' => [
'title' => 1,
'id' => 1,
'actor_details.title' => 1,
],
],
]);
/** @var \MongoDB\BSON\Document $film */
foreach ($films as $film) {
$cache = mongo()->cache();
$value = $cache->getOrSet('mykey', function() {
sleep(5); // like a API call, database query or filtering content
return 'myvalue';
}, 5);
// NOTE: we are using the cacheCollection() method here
$collection = mongo()->cacheCollection();
// find all that have the tag 'tag1' and are NOT expired
$documents = $collection->find([
'value.tags' => ['$in' => ['tag1']],
'expires_at' => ['$gt' => time()],
]);
foreach ($documents as $document) {
// $document->value->tags
// $document->value->other_value
}
// delete any cache entry older than 5 minutes
$deleteResult = $collection->deleteMany([
'expires_at' => ['$lt' => time() - 5*60]
]);
$deletedCount = $deleteResult->getDeletedCount();
// you can also manually clean expired cache entries
mongo()->clean();
return [
// ... other options
// example of using mongodb as cache driver for storing uuids
// instead of the default file-based cache
'cache' => [
'uuid' => [
'type' => 'mongodb',
],
],
];
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.