PHP Classes

File: doc/

Recommend this page to a friend!
  Classes of Xavier Pérez   XMongoDB   doc/   Download  
File: doc/
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: XMongoDB
Build and execute queries to a MongoDB database
Author: By
Last change: Chnages manual
Chnages manual
Date: 8 years ago
Size: 1,420 bytes


Class file image Download

Basic Select Operations


// Create the config class
$config = new XMongoDBConfig('localhost',27017,'mydb','myuser','mypass');

// Connect MongoDB
$xmongodb = new XMongoDB($config);


Select the fields you want to be obtanied from mongoDB collection



$xmongodb->where(array('myfield1' => 'value1', 'myfield2' => 'value2');

Important: each value must to match the type (numeric or string) of the value inserted in the mongodb collection.

See advanced where for more options...Chapter 3 - Advanced select operations

Run Query

$cursor = $xmongodb->get('mycollection');

The result is a MongoCursor object

Manipulating cursors

Once you has obtained a cursor, you can limit, skip or order results:

// Limit to 10 records max

// Skip first 20 records

// Order by name
$cursor->order_by(array('name' => 'asc'));

Getting cursors

Retrieve results and other info about the result query

// Retrive an array of objects
$result = $cursor->result();

// Retrive an array of arrays
$result = $cursor->result_array();

// Get the total of rows that accomplish conditions
$totalrows = $cursor->total_rows();

// Get only num rows retrieved
$numrows = $cursor->num_rows();