\Hazaar\MongoDB
Document
MongoDB Document class

This class can be used for inserting, updating and deleting a single document in a MongoDB collection. It has many advanced features that allow in-place ‘atomic’ updates of fields via is built-in differencing engine.

At the time of writing, this class works with MongoDB version 2.2.1.

Example

$db = new Hazaar\MongoDB(array('hosts' => 'localhost', 'database' => 'mydb')); $doc = new Hazaar\MongoDB\Document($db, 'users', array('name' => 'myusername')); $doc->address[] = array('street', 'state', 'postcode'); $doc->update();

In this example we create a new MongoDB connection object to connect to a database server on the localhost and using the ‘mydb’ database. We then find the document in the users collection where the name is ‘myusername’. After the document is loaded we add an address to the address array and then update the document.

Tags

Since

1.0.0

Summary
Methods Properties Constants
get
has
key
set
sum
$db
No constants
Properties
$collection
$collection
$collection_name
$collection_name
$db
$db
$exists
$exists
$sleep_values
$sleep_values
Methods
__construct()
__construct($db, $collection, $criteria = Array ( ) , $fields = Array ( ) )

Loading a %MongoDB Document using an existing MongoDB connection object is easy.

Tags

Since

1.0.0

Parameters

$db\Hazaar\MongoDB

A MongoDB connection object.

$collectionstring

The name of the collection to query.

$criteriaArray

Array of search criteria. See the MongoDB Documentation for information on querying.

$fieldsArray

An array of fields that you only want returned

__sleep()
__sleep() : Array
Magic method used by serialize to convert a Document object into a string.

Helpful for storing or transferring a Document class use a mechanism that doesn’t support objects.

Tags

Since

1.0.0

Returns

Array

As per the definition of the __sleep() method, we return an array of members that are safe to serialize.

__wakeup()
__wakeup()
Magic method used by unserialize to allow the Document object to be serialised into a string value.

Helpful for storing or transferring a Document class use a mechanism that doesn’t support objects.

Tags

Since

1.0.0

delete()
delete()
Delete the document from the MongoDB database

Tags

Since

1.0.0

$retval boolean True on success, false if the document could not be removed for whatever reason.

exists()
exists() : boolean
Test if the document requested on instantiation actually exists.

Tags

Since

1.0.0

Returns

boolean

True if the document exists or false if it does not.

insert()
insert() : boolean
Insert a new document into the collection.

Normally update will call this automatically if the document with ‘_id’ does not exist. You can call this method directly to force an insert. If the _id field is set then it is highly likely that the MongoDB database will throw an error.

Tags

Since

1.0.0

Returns

boolean

True on success, false otherwise.

makeDotNotation()
makeDotNotation($values, $prefix = null, $depth = null, $note = Array ( ) ) : Array
Convert a multi-dimensional array into a single dimension with dot-notation keys

Tags

Since

1.0.0

Parameters

$valuesArray

The source array to convert

$prefixstring

The current notational prefix (internal use only)

$depthint

The current depth (internal use only)

$noteArray

Recursive node list (internal use only)

Returns

Array

An single dimensional array of key/value pairs where the key is a dot notation representation of a multi-dimensional array.

resolve()
resolve() : Array
Resolves all changes that are to be made to the current MongoDB Document.

Changes are any altered values, new fields or removed fields.

Tags

Since

1.0.0

Returns

Array

An array of changed values in MongoDB ‘update format’ for use in a mongoDB MongoCollection::update() method call

resolveRecursive()
resolveRecursive($update, $values, $parent = null) : array
Recursively resolve changes to the document elements.

This looks for changes in the current document as well as recursing into any embedded documents or arrays.

Tags

Since

1.0.0

Parameters

$update

No description

$values

No description

$parent

No description

Returns

array

An array of changed elements in dot notation.

resolveRemoves()
resolveRemoves($removes, $parent = null) : Array
Resolves any removed elements to generate a $pull update on the MongoDB database.

This is because a normal $unset on an array will merely set the value to ‘null’ so we need to pull the leftover null values.

Tags

Since

1.0.0

Parameters

$removesArray

An array of values that have been removed that we need to generate $pull for.

$parentstring

The current parent node to prepend.

Returns

Array

An array of statements for use in a $pull modifier.

update()
update() : boolean
Updates the loaded document with any changes.

If the document does not exist, it will be created and the _id field will be populated.

Tags

Since

1.0.0

Returns

boolean

True if the update was successful, false otherwise.

Inherited
find()
find($criteria)

Parameters

$criteria

No description

Inherited
findOne()
findOne($criteria)

Parameters

$criteria

No description

Inherited
get()
get($key, $auto_create = true) : mixed

Method to get the current value of an element from the document.

Tags

Since

1.0.0

Parameters

$keymixed

The element key

$auto_createboolean

If the element does not exist, automatically create a new EmbeddedDocument with the requested key. Doing this allows sub-elements to be created and modified on the fly more conveniently. Default: true.

Returns

mixed

The value of the element at the key position

Inherited
getChanges()
getChanges()
Inherited
getNew()
getNew()
Inherited
getRemoves()
getRemoves()
Inherited
getValues()
getValues()

Return all values of the current document.

Inherited
has()
has($key)

Parameters

$key

No description

Inherited
hasChanges()
hasChanges()
Inherited
hasNew()
hasNew()
Inherited
hasRemoves()
hasRemoves()
Inherited
isNull()
isNull($key)

Parameters

$key

No description

Inherited
isRemoved()
isRemoved()
Inherited
key()
key()
Inherited
modify()
modify($values)

Parameters

$values

No description

Inherited
next()
next()
Inherited
offsetExists()
offsetExists($offset)

Parameters

$offset

No description

Inherited
offsetGet()
offsetGet($offset)

ArrayAccess method to get a child element.

Parameters

$offset

No description

Inherited
offsetSet()
offsetSet($offset, $value)

Parameters

$offset

No description

$value

No description

Inherited
offsetUnset()
offsetUnset($offset)

Parameters

$offset

No description

Inherited
populate()
populate($values)

Populate sets up the array with initial values.

  • This can be used to construct the initial array after it has been instatiated.
  • It can also be used to reset an array with different values

Tags

Since

1.0.0

Parameters

$valuesarray

An array of values to populate the document with

Inherited
remove()
remove($criteria)

Parameters

$criteria

No description

Inherited
reset()
reset()
Inherited
rewind()
rewind()
Inherited
set()
set($key, $value)

Parameters

$key

No description

$value

No description

Inherited
sum()
sum($criteria = Array ( ) , $fields = Array ( ) , $recursive = false)

Parameters

$criteria

No description

$fields

No description

$recursive

No description

Inherited
sumAll()
sumAll($fields, $combine = true)

Parameters

$fields

No description

$combine

No description

Inherited
toArray()
toArray($ignore_changes = false)

Parameters

$ignore_changes

No description

Inherited
valid()
valid()