Book Image

PHP and MongoDB Web Development Beginner's Guide

Book Image

PHP and MongoDB Web Development Beginner's Guide

Overview of this book

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.
Table of Contents (17 chapters)
PHP and MongoDB Web Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Time for action - creating geospatial indexes


Let's see how we can build the geospatial index on a MongoDB collection:

  1. 1. Launch the mongo interactive shell. Switch to a new database namespace called geolocation:

    $ ./mongodb/bin/mongo MongoDB shell version: 1.8.1
    connecting to: test > use geolocation switched to db geolocation >
    
  2. 2. Insert a few documents in a collection named map. Each document must contain an embedded document with two fields, latitude and longitude:

    > db.map.insert({coordinate: {latitude:23.2342987, longitude:90.20348}})
    > db.map.insert({coordinate: {latitude:23.3459835, longitude:90.92348}})
    > db.map.insert({coordinate: {latitude:23.6743521, longitude:90.30458}})
    
  3. 3. Create the geospatial index for the map collection by issuing the following command:

    >db.map.ensureIndex({coordinate: '2d'})
    
  4. 4. Enter the next command to check if the index was created:

    > db.system.indexes.find()
    { "name" : "_id_", "ns" : "geolocation.map", "key" : { "_id" : 1 }, "v" ...