OReilly MongoDB and Python (2011).pdf

(4169 KB) Pobierz
994566198.009.png
994566198.010.png
MongoDB and Python
Niall O’Higgins
Beijing
Cambridge
Farnham
Köln
Sebastopol
Tokyo
994566198.011.png 994566198.012.png 994566198.001.png 994566198.002.png 994566198.003.png 994566198.004.png 994566198.005.png 994566198.006.png
 
MongoDB and Python
by Niall O’Higgins
Copyright © 2011 Niall O’Higgins. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles ( http://my.safaribooksonline.com ) . For more information, contact our
corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com .
Editors: Mike Loukides and Shawn Wallace
Production Editor: Jasmine Perez
Proofreader: O’Reilly Production Services
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Robert Romano
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. MongoDB and Python , the image of a dwarf mongoose, and related trade dress are
trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
ISBN: 978-1-449-31037-0
[LSI]
1315837615
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v
1.
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Introduction
1
Finding Reference Documentation
2
Installing MongoDB
3
Running MongoDB
5
Setting up a Python Environment with MongoDB
6
2.
Reading and Writing to MongoDB with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Connecting to MongoDB with Python
10
Getting a Database Handle
11
Inserting a Document into a Collection
12
Write to a Collection Safely and Synchronously
13
Guaranteeing Writes to Multiple Database Nodes
14
Introduction to MongoDB Query Language
15
Reading, Counting, and Sorting Documents in a Collection
15
Updating Documents in a Collection
18
Deleting Documents from a Collection
20
MongoDB Query Operators
21
MongoDB Update Modifiers
22
3.
Common MongoDB and Python Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23
A Uniquely Document-Oriented Pattern: Embedding
23
Fast Lookups: Using Indexes with MongoDB
29
Location-based Apps with MongoDB: GeoSpatial Indexing
33
Code Defensively to Avoid KeyErrors and Other Bugs
37
Update-or-Insert: Upserts in MongoDB
39
Atomic Read-Write-Modify: MongoDB’s findAndModify
40
Fast Accounting Pattern
41
iii
994566198.007.png 994566198.008.png
Zgłoś jeśli naruszono regulamin