CRUD in Mongodb with PHP

In last post we introduced how to install mongodb and the php extension on windows.

Lets do some CRUD operation in PHP script.

Select collection

Select a collection is just like select a database:

 
$conn = new Mongo();
 
$db = $conn->selectDB('testdb');
 
$collection = $db->myc;
 

The collection can be exist or not exist, no matter what, you will get a collection, it may be the existing one or the created one.

Add document

Its almost trivial, just define an array and insert it.

 
 
$doc = array(
  'title' => 'this is title',
  'content' => "this is content",
  'category' => 'database'
);
 
$collection->insert($doc);
 
$result = $collection->find();
 
foreach($result as $doc)
    print_r($doc);
 
 

The foreach will print all the documents in collection.

 
Array
(
    [_id] => MongoId Object
        (
            [$id] => 5188bead7ab2adec27000000
        )
 
    [title] => this is title
    [content] => this is content
    [category] => database
)
Array
(
    [_id] => MongoId Object
        (
            [$id] => 5188beb77ab2ad7805000000
        )
 
    [title] => this is title
    [content] => this is content
    [category] => database
)
 

As you can see Mongodb use an unique id to identify each document .

Query document

Find a document by id:

 
$id = new MongoId('5188bebf7ab2ad7422000000');
$one = $collection->findone(array('_id' => $id));
 
print_r($one);
 

Its very similar to SQL select statements

 
 
SELECT * FROM collection WHERE _id = $id
 
 

You can also select by other keys:

 
$results = $collection->findone(array(
    'title' => 'this is title',
 
));
 

The SQL simulation

 
SELECT * FROM collection WHERE title = 'this is title'
 

How about select particular columns

 
$results = $collection->findone(array(
    'title' => 'this is title',
 
), array(
    'content'
));
echo $results['content'];
 

The second array is the columns that need to returned .

Update document

 
 
$collection->update(
    array( '_id' => new MongoId('5188bebf7ab2ad7422000000')),
    array( '$set' => array( 'content' => 'update content' ) )
);
 
 
$id = new MongoId('5188bebf7ab2ad7422000000');
$one = $collection->findone(array('_id' => $id));
 
print_r($one);
 

The $set tells Mongodb its an update, not replace the whole document.

The update can not only update the existing column, but also add new columns:

 
 
$collection->update(
    array( '_id' => new MongoId('5188bebf7ab2ad7422000000')),
    array( '$set' =>
        array( 'keywords' =>
            array( 'name', 'mongo', 'software', 'web')
        )
    )
);
 
$id = new MongoId('5188bebf7ab2ad7422000000');
$one = $collection->findone(array('_id' => $id));
 
print_r($one);
 

Output:

 
Array
(
    [_id] => MongoId Object
        (
            [$id] => 5188bebf7ab2ad7422000000
        )
 
    [category] => database
    [content] => update content
    [keywords] => Array
        (
            [0] => name
            [1] => mongo
            [2] => software
            [3] => web
        )
 
    [title] => this is title
)
 

This is not what you can easily do in a relational database.

Delete document

 
$collection->remove(array('_id'=> new MongoId('5188bebf7ab2ad7422000000')), array("justOne" => true) );