o'higgins n. - mongdb & python - 2011.pdf

(4674 KB) Pobierz
MongoDB and Python
Niall O’Higgins
Beijing
Cambridge
Farnham
Köln
Sebastopol
Tokyo
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
Finding Reference Documentation
Installing MongoDB
Running MongoDB
Setting up a Python Environment with MongoDB
1
2
3
5
6
2. Reading and Writing to MongoDB with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Connecting to MongoDB with Python
Getting a Database Handle
Inserting a Document into a Collection
Write to a Collection Safely and Synchronously
Guaranteeing Writes to Multiple Database Nodes
Introduction to MongoDB Query Language
Reading, Counting, and Sorting Documents in a Collection
Updating Documents in a Collection
Deleting Documents from a Collection
MongoDB Query Operators
MongoDB Update Modifiers
10
11
12
13
14
15
15
18
20
21
22
3. Common MongoDB and Python Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
A Uniquely Document-Oriented Pattern: Embedding
Fast Lookups: Using Indexes with MongoDB
Location-based Apps with MongoDB: GeoSpatial Indexing
Code Defensively to Avoid KeyErrors and Other Bugs
Update-or-Insert: Upserts in MongoDB
Atomic Read-Write-Modify: MongoDB’s findAndModify
Fast Accounting Pattern
23
29
33
37
39
40
41
iii
Zgłoś jeśli naruszono regulamin