Some records of the use process of shanty-Mongo

Source: Internet
Author: User
Tags zend framework mongo shell

MongoDB is used at work. A simple implementation is encapsulated based on the Progress. Of course, the existing task can be completed and an exception is found during a test, although I solved the problem by encapsulating other extension classes, I felt I was not familiar with the PHP extensions of MongoDB and MongoDB, so I found an open-source product Shanty-Mongo to learn about other implementation methods.

This open-source library is developed based on zendframework and has requirements for PHP and other products:

  • PHP 5.3 or greater
  • Zend framework 1.10.0 or greater
  • MongoDB 1.3 or greater
  • Mongo Extension from PECL

Once the above conditions are met, you can start to do it. The official website says that it has some special features and is not completely felt yet. It may be that the library has already achieved these features virtually.

  • ORM
  • Simple and flexible
  • Partial updates. Only changed data is sent back to the server. Also you can save or delete embeded documents individually.
  • Support for references (lazy loaded)
  • Support for inheritance
  • Optional schema enforcement: Validation and filtering on Properties
  • Embeded documents/documentsets can have custom document classes like the root document

From the class structure of the source code, the product encapsulates mongodbphp extended mongocollection, mongocursor, Mongo and other objects. At the same time, it implements the document of MongoDB and provides implementation classes of document and documentset; the filter and validator components of zendframeword are used to verify and check fields.


Register the namespace through ZF, add the library path to include_path at the same time, and then connect to the instance connection class. At this time, Mongo is not connected and will be connected only when the request is real.

If you are connecting to localhost without any authentication then no need to worry about connections any further. shanty Mongo will connect automatically on
First request if no connections have previusly been added.

$master = new Shanty_Mongo_Connection('mongodb://localhost:27017');Shanty_Mongo::addMaster($master);

Below we can write a document class

Create a mongo collection, and use Mongo shell: Use user. The collection is automatically created.

Create a user class. Here, only basic functions are implemented.

<?phprequire_once "Shanty/Mongo/Document.php";class Mon_User extends Shanty_Mongo_Document {    protected static $_db = 'shanty';    protected static $_collection = 'user';    protected static $_requirements = array(        'name' => 'Required',        'email' => array('Required', 'Validator:EmailAddress'),        'friends' => 'DocumentSet',        'friends.$' => array('Document:Mon_User')    );}

Compile an instance in Zend controller to test

public function indexAction() {$this->_helper->viewRenderer->setNoRender(true);try {$user = new Mon_User();$user->name = "me";$user->email = "";$user->save();} catch(Exception $e) {echo "<pre style=\"color:red;\">",$e,"</pre>";}}

In this way, save a document and go to the Mongo command line to check that dB. User. Find () has a record. modify the code below to update the record.

$user = new Mon_User();$user->update(array('name'=>'me'), array('$set' => array("email"=>"")), array('multiple'=>true));

Now you can check whether there are updates. This statement can update multiple records. This is actually the encapsulated Mongo extension, which is implemented in the class:

return static::getMongoCollection(true)->update($criteria, $object, $options);

The following describes the concepts of the document set.

$user = new Mon_User();$friends = $user->one(array('name'=>'me'), array('name', 'email'));$user->name = "me";$user->email = "";$friend = $user->friends->new();$friend->name = $friends->getProperty('name');$friend->email = $friends->getProperty('email');$user->friends->addDocument($friend);$user->save();

In this way, you can add a document set to a field.

Not complete...




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.