MongoDB & How to use it in Laravel Application
MongoDB
MongoDB is a free and open-source cross-platform document-based NoSQL database program. Unlike RDBMS it doesn’t have a schema constraint. MongoDB uses JSON-like documents with schemas.
Terminologies for MongoDB
Database
In MongoDB (or NoSQL) database is a container for collections. (unlike MySQL which contains tables).
Collections
Collections are equivalent to tables in RDBMS. But in NoSQL collections do not enforce a schema constraint (no fixed columns). A Collection contains Documents in JSON format and each document within a collection can have different fields. All similar type of documents is stored in a collection.
Document
A document is a set of key-value pair present in JSON format. A document can have any number of field and any 2 documents in the same collection can have different fields.
Document Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
{ _id: ObjectId(qwe121323qewe2wqqwqq) title: 'MongoDB with laravel', description: 'Using MongoDB in Laravel application', by: 'thewebfosters', url: 'http://www.thewebfosters.com', comments: [ { user:'user1', message: 'My first comment', like: 0 }, { user:'user2', message: 'My second comments', like: 5 } ] } |
Where to use MongoDB
MongoDB doesn’t have a schema constrain (Dynamic schema), it is fast, provided Rich queries, Highly scalable – Sharding and provides Geospatial indexing, which makes it a good choice for use in:
- Big Data
- Content Management and Delivery
- E-commerce product catalog.
- Mobile and social networking sites.
- Data hub
MongoDB with PHP & Laravel
For using MongoDB with PHP you will have to install MongoDB PHP driver – official installation guide
Laraval doesn’t have a out-of-box support for MongoDB. But thanks to the Laravel community, there is a
laravel-mongodb package which supports Eloquent model and Query builder. The best part about this package is – “It extends the original Laravel classes, so it uses exactly the same methods.”
Most of the methods are same as Eloquent query builder methods and it supports many MongoDB specific operators.
For installation and complete documentation refer to the guide here.
No Comments