PHP Classes

File: doc/2-basic-select-operations.md

Recommend this page to a friend!
  Classes of Xavier Pérez   XMongoDB   doc/2-basic-select-operations.md   Download  
File: doc/2-basic-select-operations.md
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
 

Contents

Class file image Download

Basic Select Operations

Connect

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

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

Select

Select the fields you want to be obtanied from mongoDB collection

$xmongodb->select(array('myfield1','myfield2','myfield3'));

Where

$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
$cursor->limit(10);

// Skip first 20 records
$cursor->skip(20);

// 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();