ebook img

The Definitive Guide to MongoDB The NoSQL Database for Cloud and Desktop Computing PDF

329 Pages·2010·5.18 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The Definitive Guide to MongoDB The NoSQL Database for Cloud and Desktop Computing

BOOks fOR PROfessIOnals By PROfessIOnals® The eXPeRT’s VOIce® In OPen sOuRce Companion eBook Available The Definitive Guide to MongoDB: The NoSQL Database for Cloud The Definitive Guide to and Desktop Computing The Definitive Guide to M Eelco Plugge Dear Reader, o MongoDB MongoDB is quite frankly one of the most awesome Open Source projects that we’ve worked with in the last year. Its power as a document-orientated database n and ease of use make it a very appealing proposition. The Definitive Guide to g MongoDB will take you from the very basics such as explaining what document- orientated databases are and why you would want to use them, through install- o ing and setting up MongoDB, to advanced topics on replication and sharding. D We wrote this book because we wanted to share with you how great MongoDB is and show you how your own applications can benefit from its features. To do B this, we cover how to access MongoDB from popular languages such as PHP and Python so you can start using it straight away. As we move through the book, we Peter Membrey, Author of cover essential topics such as how to store large files using the GridFS feature and The NoSQL Database for Cloud and Definitive Guide to CentOS, how to administer and optimize your MongoDB installation. Foundations of CentOS All this knowledge is put into practice in practical sample applications that act Desktop Computing as case studies of MongoDB features. You’ll soon get to grips with all aspects of MongoDB, giving you the knowledge and skills to use it in your own applications to devastating effect. We have made a great effort to ensure that, while you can read the book from cover to cover, each chapter is also completely self-contained so you can use this book as a reference as well as a way to learn MongoDB. MongoDB is a great choice for so many new and interesting projects. If you’re developing the next Amazon or Facebook, you’re going to want to know all you can about MongoDB! Eelco Plugge, Peter Membrey and Tim Hawkins Simplify the storage of complex data by Tim Hawkins creating fast and scalable databases THE APRESS ROADMAP Companion eBook Beginning Pro Definitive Guide to Python Hadoop MongoDB Beginning PHP and MySQL M H SOURCE CODE ONLINE awkinembrePlugg EelcToim P lHuagwgek,i nPseter Membrey sye and www.apress.com Shelve in Databases\General User level: Beginning–Intermediate www.it-ebooks.info > m o ok.c o b e w o w w. w w < ok o B e w! o W m o d fr a o nl w o D www.it-ebooks.info The Definitive Guide to MongoDB The NoSQL Database for Cloud and Desktop Computing ■ ■ ■ Eelco Plugge, Peter Membrey and Tim Hawkins i www.it-ebooks.info The Definitive Guide to MongoDB: The NoSQL Database for Cloud and Desktop Computing Copyright © 2010 by Eelco Plugge, Peter Membrey and Tim Hawkins All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-3051-9 ISBN-13 (electronic): 978-1-4302-3052-6 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. President and Publisher: Paul Manning Lead Editors: Frank Pohlmann, Michelle Lowman, James Markham Technical Reviewer: Jonathon Drewett Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Mary Tobin Copy Editor: Patrick Meader Compositor: MacPS, LLC Indexer: Potomac Indexing, LLC Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail(cid:1)[email protected], or(cid:1) visit www.springeronline.com(cid:2)(cid:1)(cid:1) For information on translations, please e-mail [email protected],(cid:1)(cid:3)(cid:4)(cid:1)(cid:7)i(cid:5)i(cid:6)(cid:1)www.apress.com.(cid:1)(cid:1) Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at(cid:1)www.apress.com/info/bulksales(cid:2)(cid:1) The information in this book is distributed on an (cid:8)as is(cid:9) basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at(cid:1)www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. ii www.it-ebooks.info For the love of my life, Marjolein, and my son Jesse—I wouldn’t have been able to write this without your everlasting patience and love. —Eelco Plugge For my mother-in-law, Wan Ha Loi. First for actually letting me marry her wonderful daughter and second for coming out of retirement to look after our son Kaydyn. Her selfless generosity made this book possible, as, without her continuous support, there simply wouldn’t be enough hours in the day. —Peter Membrey For Ester, for putting up with the long hours I stole from her to produce this book. —Tim Hawkins iii www.it-ebooks.info Contents at a Glance ■Contents................................................................................................................v(cid:1) ■About the Authors..............................................................................................xvi(cid:1) ■About the Technical Reviewer...........................................................................xvii(cid:1) ■Acknowledgments............................................................................................xviii(cid:1) ■Introduction ........................................................................................................xx Part I: Basics............................................................................................................1(cid:1) ■Chapter 1: Introduction to MongoDB.....................................................................3(cid:1) ■Chapter 2: Installing MongoDB ...........................................................................19(cid:1) ■Chapter 3: The Data Model..................................................................................35(cid:1) ■Chapter 4: Working with Data.............................................................................47(cid:1) ■Chapter 5: GridFS................................................................................................83(cid:1) Part II: Developing..................................................................................................97(cid:1) ■Chapter 6: PHP and MongoDB.............................................................................99(cid:1) ■Chapter 7: Python and MongoDB......................................................................137(cid:1) ■Chapter 8: Creating a Blog Application with the PHP Driver.............................167(cid:1) Part III: Advanced.................................................................................................191(cid:1) ■Chapter 9: Database Administration.................................................................193(cid:1) ■Chapter 10: Optimization..................................................................................225(cid:1) ■Chapter 11: Replication.....................................................................................241(cid:1) ■Chapter 12: Sharding........................................................................................277(cid:1) ■Index.................................................................................................................293(cid:1) iv www.it-ebooks.info Contents ■Contents at a Glance............................................................................................iv(cid:1) ■About the Authors..............................................................................................xvi(cid:1) ■About the Technical Reviewer...........................................................................xvii(cid:1) ■Acknowledgments............................................................................................xviii(cid:1) ■Introduction.........................................................................................................xx (cid:1) Part I: Basics............................................................................................................1(cid:1) ■Chapter 1: Introduction to MongoDB.....................................................................3(cid:1) Reviewing the MongoDB Philosophy..............................................................................3(cid:1) Using the Right Tool for the Right Job...................................................................................................3(cid:1) Lacking Innate Support for Transactions...............................................................................................5(cid:1) Drilling Down on JSON and How It Relates to MongoDB.......................................................................5(cid:1) Adopting a Non-Relational Approach.....................................................................................................7(cid:1) Opting for Performance vs. Features.....................................................................................................8(cid:1) Running the Database Anywhere...........................................................................................................9(cid:1) Fitting Everything Together.............................................................................................9(cid:1) Generating or Creating a Key.................................................................................................................9(cid:1) Using Keys and Values.........................................................................................................................10(cid:1) Implementing Collections.....................................................................................................................11(cid:1) Understanding Databases....................................................................................................................11(cid:1) Reviewing the Feature List...........................................................................................11(cid:1) Using Document-Orientated Storage (BSON).......................................................................................11(cid:1) Supporting Dynamic Queries...............................................................................................................12 v www.it-ebooks.info ■ CONTENTS Indexing Your Documents....................................................................................................................13(cid:1) Leveraging Geospatial Indexes............................................................................................................13(cid:1) Profiling Queries...................................................................................................................................14(cid:1) Updating Information In-Place.............................................................................................................14(cid:1) Storing Binary Data..............................................................................................................................14(cid:1) Replicating Data...................................................................................................................................15(cid:1) Implementing Auto Sharding...............................................................................................................15(cid:1) Using Map and Reduce Functions........................................................................................................16(cid:1) Getting Help..................................................................................................................16(cid:1) Visiting the Website.............................................................................................................................16(cid:1) Chatting with the MongoDB Developers..............................................................................................16(cid:1) Cutting and Pasting MongoDB Code....................................................................................................17(cid:1) Finding Solutions on Google Groups....................................................................................................17(cid:1) Leveraging the JIRA Tracking System.................................................................................................17(cid:1) Summary.......................................................................................................................17(cid:1) ■Chapter 2: Installing MongoDB ...........................................................................19(cid:1) Choosing Your Version..................................................................................................19(cid:1) Understanding the Version Numbers...................................................................................................20(cid:1) Installing MongoDB on Your System.............................................................................20(cid:1) Installing MongoDB Under Linux..........................................................................................................20(cid:1) Installing MongoDB Under Windows....................................................................................................22(cid:1) Running MongoDB........................................................................................................22(cid:1) Prerequisites........................................................................................................................................22(cid:1) Surveying the Installation Layout.........................................................................................................23(cid:1) Using the MongoDB Shell.....................................................................................................................23(cid:1) Installing Additional Drivers..........................................................................................24(cid:1) Installing the PHP driver.......................................................................................................................25(cid:1) Confirming Your PHP Installation Works..............................................................................................28(cid:1) Installing the Python Driver..................................................................................................................30(cid:1) Confirming Your PyMongo Installation Works......................................................................................33(cid:1) vi www.it-ebooks.info ■ CONTENTS Summary.......................................................................................................................33(cid:1) ■Chapter 3: The Data Model..................................................................................35(cid:1) Designing the Database................................................................................................35(cid:1) Drilling Down on Collections................................................................................................................36(cid:1) Using Documents.................................................................................................................................38(cid:1) Creating the _id Field...........................................................................................................................40(cid:1) Building Indexes............................................................................................................41(cid:1) Impacting Performance with Indexes..................................................................................................42(cid:1) Implementing Geospatial Indexing................................................................................42(cid:1) Querying Geospatial Information..........................................................................................................43(cid:1) Using MongoDB in the Real World................................................................................46(cid:1) Summary.......................................................................................................................46(cid:1) ■Chapter 4: Working with Data.............................................................................47(cid:1) Navigating Your Databases...........................................................................................47(cid:1) Viewing Available Databases and Collections......................................................................................47(cid:1) Inserting Data into Collections......................................................................................48(cid:1) Querying for Data..........................................................................................................49(cid:1) Using the Dot Notation.........................................................................................................................51(cid:1) Using the Sort, Limit, and Skip Functions............................................................................................52(cid:1) Working with Capped Collections, Natural Order, and $natural...........................................................53(cid:1) Retrieving a Single Document..............................................................................................................55(cid:1) Using the Aggregation Commands.......................................................................................................55(cid:1) Working with Conditional Operators....................................................................................................57(cid:1) Leveraging Regular Expressions..........................................................................................................65(cid:1) Updating Data...............................................................................................................65(cid:1) Updating with update().........................................................................................................................65(cid:1) Implementing an Upsert with the save() Command.............................................................................66(cid:1) Updating Information Automatically.....................................................................................................66(cid:1) Specifying the Position of a Matched Array.........................................................................................70(cid:1) vii www.it-ebooks.info ■ CONTENTS Atomic Operations................................................................................................................................71(cid:1) Modifying and Returning a Document Atomically................................................................................73(cid:1) Renaming a Collection..................................................................................................74(cid:1) Removing Data..............................................................................................................74(cid:1) Referencing a Database................................................................................................75(cid:1) Referencing Data Manually..................................................................................................................75(cid:1) Referencing Data with DBRef...............................................................................................................76(cid:1) Implementing Index-Related Functions........................................................................78(cid:1) Surveying Index-Related Commands...................................................................................................80(cid:1) Forcing a Specified Index to Query Data..............................................................................................80(cid:1) Constraining Query Matches................................................................................................................80(cid:1) Summary.......................................................................................................................81(cid:1) ■Chapter 5: GridFS................................................................................................83(cid:1) Filling in Some Background..........................................................................................83(cid:1) Working with GridFS.....................................................................................................84(cid:1) Getting Started with the Command-Line Tools.............................................................85(cid:1) Using the _id Key.................................................................................................................................86(cid:1) Working with Filenames......................................................................................................................86(cid:1) Determining a File’s Length.................................................................................................................86(cid:1) Working with Chunk Sizes...................................................................................................................87(cid:1) Tracking the Upload Date.....................................................................................................................87(cid:1) Hashing Your Files...............................................................................................................................87(cid:1) Looking Under MongoDB’s Hood..................................................................................88(cid:1) Using the Search Command.................................................................................................................90(cid:1) Deleting................................................................................................................................................90(cid:1) Retrieving Files from MongoDB...........................................................................................................91(cid:1) Summing up mongofiles......................................................................................................................91(cid:1) Exploiting the Power of Python.....................................................................................91(cid:1) Connecting to the Database.................................................................................................................92(cid:1) viii www.it-ebooks.info

Description:
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity Implementing Index-Related Functions . If you pick up any book that teaches PHP (such as PHP for Absolute Beginners (Apress, 2009)) by. Jason Lengstorf
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.