MongoDB

Class \Lib\Mongo

Methods

All the methods and indications to integrate with Mongo instances.

🚧

Note interne APPSPANEL

Ce mettre d'accord à quelle(s)s collection(s) ont donne accès les développeurs externes, quelle est leur liberté par rapport à Mongo, car c'est un point assez critique

📘

Note

[appliname]_groups
[appliname]_users

getInstance

<?php public static function \Lib\Mongo::getInstance()

Description
Method that creates the unique instance of the class if it does not already exist and then returns it.

Return
self::$instance


insert

<?php public function insert(string $db_name = '', string $collection_name = '', Array $data = array())

Description
Selects a collection and insert data.

Parameters

  • db_name
    The database name
  • collection_name
    The collection name
  • data
    An array or object. If an object is used, it may not have protected or private properties.

update

<?php public function update(string $db_name = '', string $collection_name = '', Array $where = array(), Array $data = array(),

Description
Selects a collection and update records based on a given criteria

Parameters

  • db_name
    The database name
  • collection_name
    The collection name
  • where
    Query criteria for the documents to update.
  • data
    The object used to update the matched documents. This may either contain update operators (for modifying specific fields) or be a replacement document.

find

<?php public function find(string $db_name = '', string $collection_name = '', Array $where = array(), Boolean $single = false)

Description
Selects a collection, queries this collection and returning a MongoCursor for the result set.

Parameters

  • db_name
    The database name
  • collection_name
    The collection name
  • where
    Query criteria for the documents to update.
  • single
    If single is true, returns only one result set.

Return
Returns a cursor for the search results.


remove

<?php public function remove(string $db_name = '', string $collection_name = '', Array $where = array())

Description
Selects a collection and remove records from this collection.

Parameters

  • db_name
    The database name
  • collection_name
    The collection name
  • where
    Query criteria for the documents to update.

Examples

Find and return a user token stored in MongoDB

<?php

$mgr = \Lib\Mongo::getInstance();

// get the name of application
$applicationName = \Lib\Application::getInstance()->get('name');

// Build the query
$query = array(
  'APPS' => $applicationName,
  'TOKEN' => $token
);

\Lib\Log::info("query : " . json_encode($query));

// Find the token user
$cursor = $mgr->find(
  'test',
  sprintf(
    '%s_users',
    $applicationName
  ),
  $query
);

if ($cursor->count() > 0) {
  // Sort by REQUEST_TIME desc to get the last one
  $cursor->sort(array('REQUEST_TIME' => -1));
  $document = $cursor->getNext();

  // Handle expiration
  if (!empty($document['EXPIRATION']) && time() > $document['EXPIRATION']) {
    \Lib\Log::info('[Token] Token expiré.');
    return null;
  }

  \Lib\Log::info("document : " . json_encode($document));

  return $document;

} else {
  return null;
}
return $result;