Skip Navigation LinksHome > Categories > Code from a Category

MongoDb and PHP

User Name: serfcompany
Name: Serf
Contact Me:
Home Page:
php,mysql,javascript,html,css. Preferable working with Zend Framework. Good know javascript. I worked with various, javascript frameworks such as(jquery, YUI3, extjs, sencha touch). [More]
Viewed Times: 1706
Add Date: 02/19/2012
NoSQL - what a strange acronym? Probably every one of you have heard the acronym, you may even be "fingered" her by heart, and maybe use it in their projects on the production servers. The technology at the time of its appearance caused a a lot of noise, easily squeezed, and selecting a piece of bread from a relational database. NoSQL term means that it is not only (not only) SQL, although at first glance it seems that this is not SQL (no). The term means a variety of technologies to work with databases to bypass the standard relational model. Data can be stored as key-value pairs, trees, or documents. We'll look at the last.

Description of document-oriented storage

In this system, a database management unit of information storage is a document that is stored in the form of key-to-one and have some respect.

What is MongoDB?

Before considering possibilities of a great product I would like to mention six key concepts contained in it:

1.The concept of a database is not distinguishable from the concepts of relational databases in a DBMS. MongoDB also can contain zero or more databases that are top-level container.

2.The database can contain zero or more collections. Collections in the context of an RDBMS (hereinafter the system relational database management) is a table.

3.A collection can contain documents. In an RDBMS is a string.

4.A document can consist of different number of fields. To draw an analogy with the relational model, it is certainly a column.

5.The indices are in MongoDB, and in the RDBMS, the functions are almost inseparable.

6.Cursors are in MongoDB, but the direct counterpart in the relational model does not. It is important to understand that when you request a refund you MongoDB certain data, Mongo returns a cursor, with which we can calculate, or to skip some of the documents, no load on the server.

Installing MongoDB

Details of the installation are widely represented on the official site, so we will not focus on this topic. I will touch on only the main points to get started.

Walk through the steps to complete it:

  • Go to the official download page, download the appropriate version of the compiled.(
  • Unpack the archive anywhere and go in the folder bin. mongod - server, mongo - the client shell - with these two sources we will spend most of the time.
  • Create a text file in the bin, let it be known mongod.conf
  • Write one line to the newly created file dbpath = WAY-where-you-WANT-TO-BASE-lying DATA. For example, on Windows, you can write dbpath = c: \ mongodb \ data, under Linux dbpath = / etc / mongodb / data. The specified path must exist (make sure about this).
  • Start from the bin file with a parameter mongod **-config / path / to / config / mongodb.conf.

  • Installation is complete. To test this, run the command line client (mongo), which should automatically connect to a running server and display the prompt. Try typing ** help ** or db.version (). While our clients will leave and move on to PHP.

    PHP and MongoDB

    MongoDB currently has the support of almost all the popular programming languages such as the C + +, Erlang, Java, Javascript, Perl, Python and PHP. At last we stop.

    File extension

    I will not give an example set of extensions for PHP. There is nothing trivial. It is better focused on what-nibu more interesting to you after reading this note, it is easy to write your first application using MongoDB. A complete description of the extension, you can visit the official guide (


    Let's start with the connection (the server at the moment must be running). If we are experimenting on their local machine, then it's simple.

    If you are using MongoDB on a dedicated hosting, or for some other reason you need to login, this example is slightly more complicated.

    These dances are very similar to the DSN, which is commonly used with the PDO.


    Connect to the server, we have learned, as well as to choose the collection. By the way, if there is no collection, do not worry if you make a non-existent data collection, careful Mongo will create it for you. Once again I am convinced that MongoDB created for lazy people who want to focus only on the important things, but not at the dances with a tambourine.


    The sample is almost as well as in relational databases, after the word WHERE (select * from COLLECTION-NAME WHERE alias = 'about' LIMIT 1)
    The developers have provided a lot of buns in the sample, but all of them to consider in this introductory note does not seem possible, so tell all of the sample with regular expressions, and are rejected.

    This example is nearly out of the real project, which shows both the selection of a regular expression, and work with cursors.
    I hope this code will not cause you trouble.


    Change the data in the documents a little more extensive section encompassing and sample and record new data.
    But not all that difficult.
    I'll start with an example.

    This confusing at first glance a record is really easy if you break it into components.
    The first parameter in the method update () responsible for sampling, ie selection criteria, the second parameter, as will be changed.
    In this case, we set ($ set) the title of the new.
    Different from the $ set parameters can be found in the documentation for MongoDB.
    Analogous to the SQL might look like

    This minimum, as I mentioned earlier, should be sufficient to implement a simple application.
    All information other than I mentioned in this article can be found on the official site MongoDB


    First of all you should understand that there is no magic pill.
    Any decision, whatever it may seem from a flexible and easy has its pluses and minuses.
    If you do not need to store the statistical data that smears of tens and even hundreds of tables with complex relationships, it is unlikely you will approach NoSQL solutions.
    If the data are easily de-normalization, feel free to choose NoSQL because of the speed and convenient operation.

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.