Combine the power of PHP and MongoDB to build dynamic web 2.0 applications In Detail With the rise of Web 2.0, the need for a highly scalable database, capable of storing diverse user-generated content is increasing. MongoDB, an open-source, non-relational database has stepped up to meet this demand and is being used in some of the most popular websites in the world. MongoDB is one of the NoSQL databases which is gaining popularity for developing PHP Web 2.0 applications. PHP and MongoDB Web Development Beginner's Guide is a fast-paced, hands-on guide to get started with web application development using PHP and MongoDB. The book follows a "Code first, explain later" approach, using practical examples in PHP to demonstrate unique features of MongoDB. It does not overwhelm you with information (or starve you of it), but gives you enough to get a solid practical grasp on the concepts. The book starts by introducing the underlying concepts of MongoDB. Each chapter contains practical examples in PHP that teache specific features of the database. The book teaches you to build a blogging application, handle user sessions and authentication, and perform aggregation with MapReduce. You will learn unique MongoDB features and solve interesting problems like real-time analytics, location-aware web apps etc. You will be guided to use MongoDB alongside MySQL to build a diverse data back-end. With its concise coverage of concepts and numerous practical examples, PHP and MongoDB Web Development Beginner's Guide is the right choice for the PHP developer to get started with learning MongoDB. What you will learn from this book Approach The book follows a "Code first, explain later" approach, using practical examples in PHP to demonstrate unique features of MongoDB. This book is packed with step-by-step instructions and practical examples, along with challenges to test and improve your knowledge. Who this book is written for This book assumes that you are experienced in web application development using PHP, HTML, and CSS. Having working knowledge of using a relational database system such as MySQL will help you grasp some of the concepts quicker, but it is not strictly mandatory. No prior knowledge of MongoDB is required.
Disclaimer: ho ricevuto il libro per la recensione da Packt Publishing stessa. Ho cercato comunque di fare una recensione obiettiva.
Recensione generale
In una parola: ottimo. Anche se migliorabile.
A memoria credo sia il primo a trattare l'integrazione tra PHP e MongoDB. Lo stile è molto scorrevole. I concetti sono spiegati in modo chiaro nella maggior parte dei casi. Fanno eccezione i concetti di programmazione funzionale nel capitolo dedicato a map-reduce, che avrebbero beneficiato di un maggiore approfondimento. Ogni capitolo presenta diversi esempi di codice per gli argomenti trattati. I quiz di cui è costellato aiutano a richiamare i concetti appena appresi e a capire se sia il caso di rileggere quanto appena studiato. Peccato che non siano molto numerosi. Le soluzioni ai quiz si trovano alla fine del libro.
Vengono presentati anche alcuni "compiti per casa" (di cui non viene data soluzione, naturalmente) che invitano ad implementare soluzioni diverse da quelle presentate o software che utilizzando i concetti esposti li espandano per creare software più complessi.
La caratteristica che ho apprezzato di più è la presentazione degli esempi: prima viene esposto il codice completo di tutti gli script, e in seguito, durante la spiegazione, vengono riportati i frammenti di codice. Questo consente di avere una visione globale e di poterla studiare da soli. Molti altri testi spezzano i sorgenti rendendoli illeggibili, o integrano la spiegazione in commenti lunghissimi che, senza colorazione della sintassi, li rendono altrettanto illeggibili.
Lo stile adottato sembra distaccarsi un po' dal classico dei manuali tecnici, per tendere a quello dei testi universitari.
In alcuni casi si ha la sensazione che ci sia molto di più dietro (e spesso a ragione), e che il libro copra solo l'uso base di alcune caratteristiche. In altri casi è scritto chiaramente (con relativo link) che per approfondire si possono consultare fonti online.
Alcuni piccoli errori tipografici come campi di array separati da : (come in javascript) invece che da => in PHP, o virgolette dimenticate nelle chiavi di accesso agli array sono facilmente individuabili e non pregiudicano la qualità del libro.
Capitolo 1 - Installazione
Inizia con una breve panoramica del "movimento NoSQL", ne spiega brevemente i fondamenti, e poi passa alla procedura di installazione vera e propria, sia per Windows che per Linux, mentre per OSX dice semplicemente "è come su Linux". In tutti i casi la procedura è tanto semplice da richiedere non più di un paio di pagine. Passa velocemente all'avvio del server, coprendo le principali opzioni di configurazione, alla creazione di database e collection, per finire con l'installazione, tramite PEAR, dei driver per PHP e a un primo script dimostrativo.
Capitolo 2 - La prima applicazione
Ormai diventato l'"hello world" dei libri che trattano web, si parte costruendo un piccolo blog senza troppe pretese: inserimento, cancellazione, modifica degli articoli. Quindi si passa ad approfondire tramite paginazione dell'archivio per finire con l'inserimento di commenti ai post. Durante il percorso vengono spiegate le classiche operazioni CRUD (Create, Read, Update, Delete), spiegando anche il particolare linguaggio di query di MongoDB, le operazioni di limit e skip, il concetto di cursore e di scrittura safe. Si conclude con alcune operazioni particolari (upsert, incremento di un campo, rimozione di un campo) e con la distinzione tra documenti embedded e esterni, spiegando anche come fare le "select" cercando dati su documenti embedded.
Capitolo 3 - Session Manager
Dopo una breve introduzione a cosa sia una sessione, passa subito a definire un session handler personalizzato. Con l'occasione viene anche creata una classe (singleton) per la connessione al DB, che verrà utilizzata nel resto del libro. Viene quindi creata una classe per la gestione della sessione tramite MongoDB, facilmente riutilizzabile in altri progetti in quanto quasi totalmente trasparente. Il capitolo si conclude con un esempio di gestione di login/logout degli utenti. Molto utile non solo per capire come come astrarre l'accesso a MongoDB tramite una classe e come usare upsert, ma anche per capire come usare un DB generico (anche SQL) per le sessioni in PHP.
Capitolo 4 e 5 - Aggregazioni e statistiche
Accorpo la descrizione dei due capitoli perché trattano più o meno lo stesso argomento: map-reduce e group(). Il primo è forse l'argomento più difficile per chi è abituato a linguaggi procedurali o ad oggetti, in quanto riprende concetti della programmazione funzionale, mentre il secondo si avvicina di più a concetti conosciuti come quelli dell'SQL. Due capitoli non facili da assimilare, nei quali avrei forse visto bene un numero maggiore di esempi. Fortunatamente quelli che ci sono riguardano applicazioni pratiche (tag cloud, statistiche di accesso alle pagine, ecc.) e non argomenti astratti. Vengono introdotte anche le capped collections.
Capitolo 6 - Integrazione con i RDBMS
Forse il capitolo meno interessante per me, anche se tratta un argomento importante. Vengono portati diversi esempi di come usare in parallelo MongoDB e un database SQL (MySQL): come usare MongoDB per fare cache delle query complesse fatte su MySQL, o per archiviare vecchi dati in modo da scaricare MySQL.
Capitolo 7 - GridFS
Capitolo un po' scarno, ma che fornisce tutte le informazioni necessarie per iniziare a lavorare con GridFS per lo storage di file dentro il DB. Anche qui MongoDB eccelle per semplicità, quindi gli esempi sono brevi e chiari. Uno degli esercizi invita ad implementare una gallery con thumbnail, dicendo di salvare le thumbnail assieme all'immagine originale, ma non spiega se si possano avere due file nello stesso ducumento (hint: non si può)
Capitolo 8: Geolocalizzazione
Introdu ce le funzionalità di geolocalizzazione di MongoDB, fornite sia da indici bidimensionali che da procedure predefinite nel server per la ricerca in base alla distanza da un punto. Una buona introduzione, anche se lascia un po' il dubbio sulle unità di misura (gradi, radianti, metri?) che vanno utilizzate. Ottima la spiegazione di come integrare queste funzionalità con le mappe di Google per la visualizzazione dei risultati.
Capitolo 9: Sicurezza
Questo è il capitolo con meno codice PHP. Si concentra sull'amministrazione dalla console di MongoDB per la creazione di utenti con relative password, e di vari tipi di indice, comprese le procedure per analizzarne l'efficacia. L'argomento indici forse sarebbe stato meglio trattato in un capitolo precedente.
Capitolo 10: RockMongo e phpMoAdmin
Una rapida panoramica di due interfacce web (scritte in PHP) per l'amministrazione, l'interrogazione e, soprattutto, il backup e il restore di database o di collections. Copre pro e contro delle due soluzioni, l'installazione e l'uso di base.
----- English version -----
Disclaimer: I received the book from Packt Publishing to review it. I tried hard to be objective.
General review
In a word: excellent. But it can be improved.
If I recall correctly, this is the first book about integrating PHP and MongoDB. The writing style is easy and most concepts are clearly explained. Some dark spots remain in the functional programming concepts in the map-reduce dedicated chapter. A deeper explanation could have helped. Every chapter sports various code examples and quizzes scattered through the books help recalling just-read concepts. Maybe they are a little too few. Solutions to the quizzes are in the appendix.
Every chapter also has "call to actions" (without solutions, naturally) inviting the users to implement solutions based on explained concepts.
My preferred part of the books are the examples: the code is presented full and complete, and only after that, you can read the explanation, with snippets taken from it. You can have a full view of the code and analyse it by yourself, or read the description snippet by snippet. Most other technical manuals split the code, or fill it with comments, making it nearly unreadable.
The book style appears to shift a little away from classical technical manual, reminding academic books.
Sometimes it seems (often rightly) there is much more behind some functionalities and that the book teaches only the basic usage. In some cases it is clearly written (including a link) that you can deepen your understanding reading online articles.
Some minor typos, like array fields defined by : (as in javascript) instead of => in PHP code, or missing quotes in array indexes, are easily spotted and don't lower the quality.
Chapter 1: installation
After a brief introduction to the NoSQL movement explaining its fundamentals, installation on Windows and Linux is described. As for OSX, it simply says "it is just like on Linux". Installation is so easy that it takes at most a couple of pages. Next comes server startup, covering base configuration options, and basic database and collections creation. It ends with PHP driver installation (using PEAR) and a little example script.
Chapter 2: first application
It became the "hello world" of books covering web techs: you begin building a simple blog, covering insert, delete and alter operations. Then pagination and comments are introduced. It serves its purpouse in introducing CRUD (Create, Read, Update, Delete) operations in MongoDB, covering MongoDB query language, including limit, skip, cursors and safe writing concepts. The chapter ends with some peculiar operations (upserts, field increment, field removal) and the distinction between embedded and referenced document, also explaining how to do a "select" operation on embedded documents.
Chapter 3: a session manager
After a brief introduction, the book delves into creating a custom session handler. A (singleton) class is introduced to manage the connection to the DB, and it will be used throughout the entire book. A class is defined to handle the PHP session using a MongoDB collection. This can easily be reused in own projects, being nearly completely transparent. The chapter ends with a user login/logout example. It is useful to understand how to abstract access to MongoDB through a class, how to use upsert and also to know how to use a generic (even SQL) database to handle sessions.
Chapters 4 and 5: aggregation and statistics
I cover the two chapters together because they deal with the same topics: map-reduce and group(). Map-reduce is maybe the hardest topic for thos used to procedural or OOP languages, as it embraces function programming concepts. On the other hand, group() is similar to the SQL GROUP BY concept. These chapters' arguments are not easy to grasp, and I would have liked more examples. Luckily the given examples are very practical (tag cloud, web access statistics, ...) and not abstract aggregations. Capped collections are introduced here.
Chapter 6: RDBMS integration
This is maybe the least useful chapter, in my opinion, but it could be important to others. Several examples of how to connect to a SQL (MySQL) DB and MongoDB in parallel: how to use MongoDB as a query cache, or to archive old data, so to remove burden from MySQL queries.
Chapter 7: GridFS
This chapter is a little thin, given the topic, but it gives you everything you need to start working with GridFS to store (binary) files in the database. MongoDB excels in its simplicity, and the examples are short and clear. One of the "call to actions" asks to implement a thumbnail gallery, saving the thumbnail along with the original image, but does not explain if you can save different files in the same document (hint: you can't).
Chapter 8: geolocation
This chapter introduces MongoDB's geolocation features: 2D indexes and server-predefined procedures to search distance-based data. A good introduction, even if it leaves some itches to scratch about measure units (degrees? radians? meters?). Excellent explanation on how to use the data with Google Maps to visualize the results.
Chapter 9: security
The chapter with fewer PHP code lines in it, focuses on console administration for user (and password) access management. It also covers indexes creation and analysis: this part should have been introduced earlier in the book.
Chapter 10: RockMongo and phpMoAdmin
A quick introduction to two web-based (PHP) interfaces for administration query and, above all, backup and restore of databases and collections. Covers pros and cons of both solutions, as well as installation and basic use.
I found this book to be very informative for someone who is interested in MongoDB and wants to learn the type of database architecture MongoDB supports. This book has a web-centric view where it shows step-by-step examples that anyone can follow. Every chapter has a review of what is covered in the chapter, and plenty of examples to illustrate the author’s points.
My take on this subject is from the Database Administrator point of view, and here I found the book to be a little light on substance, but this book is clearly not marketed to that segment but to developers. Some of the advanced topics covered in this book are MapReduce, GridFS and Geospatial location and on these topics this book excels.
This book would be a fine addition to any bookshelf and will have a special place in mine. It clearly shows the power of MongoDB and PHP in a Web 2.0 environment and would be a great reference to anyone in Web development.
This was a great book for those looking to get their feet wet with MongoDB. PHP And MongoDB covered many more topics than many of the MongoDB books I have read recently and while I am not a PHP developer gave me a few more ways to leverage MongoDB in my day to day work.
I especially enjoyed the chapters on Geospatial functionality and the GridFS system. Both of these topics we handled throughly and are typically glossed over in other books.
The one place I felt this book was light was the operations and administration side of things. Nowadays, many developers are handling operations as well and I feel that those topics could be explored further in books like this.
All in all, I would recommend anyone in web development looking for somewhere to start with MongoDB pick up this book and give it a read.
Very good book to introduce you to the PHP & MongoDB technologies , there is a lot of practical examples with explanations covering the main concepts with NoSQL development.