forked from UWPCE-PythonCert/ProgrammingInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoSQL.html
More file actions
505 lines (489 loc) · 31.2 KB
/
NoSQL.html
File metadata and controls
505 lines (489 loc) · 31.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>No SQL Databases — Programming in Python 7.0 documentation</title>
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Graph Databases" href="GraphDatabases.html" />
<link rel="prev" title="Debugging" href="Debugging.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" style="background: #4b2e83" >
<a href="../index.html">
<img src="../_static/UWPCE_logo_full.png" class="logo" alt="Logo"/>
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<p class="caption" role="heading"><span class="caption-text">Topics in the Program</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../topics/01-setting_up/index.html">1. Setting up your Environment</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/02-basic_python/index.html">2. Basic Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/03-recursion_booleans/index.html">3. Booleans and Recursion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/04-sequences_iteration/index.html">4. Sequences and Iteration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/05-text_handling/index.html">5. Basic Text Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/06-exceptions/index.html">6. Exception Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/07-unit_testing/index.html">7. Unit Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/08-dicts_sets/index.html">8. Dictionaries and Sets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/09-files/index.html">9. File Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/10-modules_packages/index.html">10. Modules and Packages</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/11-argument_passing/index.html">11. Advanced Argument Passing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/12-comprehensions/index.html">12. Comprehensions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/13-intro_oo/index.html">13. Intro to Object Oriented Programing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/14-magic_methods/index.html">14. Properties and Magic Methods</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/15-subclassing/index.html">15. Subclassing and Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/16-multiple_inheritance/index.html">16. Multiple Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/17-functional_programming/index.html">17. Introduction to Functional Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/18-advanced_testing/index.html">18. Advanced Testing</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../topics/99-extras/index.html">19. Extra Topics</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="Pep8.html">Coding Style and Linting</a></li>
<li class="toctree-l2"><a class="reference internal" href="CodeReviews.html">Code Reviews</a></li>
<li class="toctree-l2"><a class="reference internal" href="PersistanceAndSerialization.html">Persistence and Serialization</a></li>
<li class="toctree-l2"><a class="reference internal" href="Unicode.html">Unicode in Python</a></li>
<li class="toctree-l2"><a class="reference internal" href="IteratorsAndGenerators.html">Iterators and Generators</a></li>
<li class="toctree-l2"><a class="reference internal" href="Decorators.html">Decorators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/mailroom/mailroom-decorator.html">Mailroom – Decoratoring it</a></li>
<li class="toctree-l2"><a class="reference internal" href="ContextManagers.html">Context Managers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/context-managers-exercise.html">A Couple Handy Context Managers</a></li>
<li class="toctree-l2"><a class="reference internal" href="MetaProgramming.html">Metaprogramming</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/mailroom/mailroom-meta.html">Mailroom – metaprogramming it!</a></li>
<li class="toctree-l2"><a class="reference internal" href="Logging.html">Logging and the logging module</a></li>
<li class="toctree-l2"><a class="reference internal" href="Debugging.html">Debugging</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">No SQL Databases</a></li>
<li class="toctree-l2"><a class="reference internal" href="GraphDatabases.html">Graph Databases</a></li>
<li class="toctree-l2"><a class="reference internal" href="Concurrency.html">Concurrent Programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="Async.html">Asychronous Programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="Coroutines.html">Notes on Coroutines</a></li>
<li class="toctree-l2"><a class="reference internal" href="ThreadingMultiprocessing.html">Threading and multiprocessing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/threaded_downloader.html">Threaded Web Scraper</a></li>
<li class="toctree-l2"><a class="reference internal" href="Profiling.html">Performance and Profiling</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" style="background: #4b2e83" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Programming in Python</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content style-external-links">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> »</li>
<li><a href="../topics/99-extras/index.html"><span class="section-number">19. </span>Extra Topics</a> »</li>
<li>No SQL Databases</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/modules/NoSQL.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul><div class="rst-breadcrumbs-buttons" role="navigation" aria-label="Sequential page navigation">
<a href="Debugging.html" class="btn btn-neutral float-left" title="Debugging" accesskey="p"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="GraphDatabases.html" class="btn btn-neutral float-right" title="Graph Databases" accesskey="n">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="no-sql-databases">
<span id="nosql"></span><h1>No SQL Databases<a class="headerlink" href="#no-sql-databases" title="Permalink to this headline"></a></h1>
<div class="section" id="no-sql">
<h2>“No SQL”?<a class="headerlink" href="#no-sql" title="Permalink to this headline"></a></h2>
<p>Structured Query Language (SQL) is the standard language for communicating with relational database management systems (RDBMS).</p>
<p>But an RDBMS system is not always the best way to store your data.</p>
<p>There are other alternatives, each with there own approach, but as RDBMSs and SQL are so ubiquitous, they are all lumped in under the moniker “NoSQL”.</p>
<p>I personally hate things that are defined by what they are NOT, rather than what they are, but that’s the terminology these days.</p>
<div class="section" id="what-is-a-database">
<h3>What is a Database?<a class="headerlink" href="#what-is-a-database" title="Permalink to this headline"></a></h3>
<p>A database is an organized collection of data. The data are typically organized to model relevant aspects of reality in a way that supports processes requiring this information.</p>
<p>Usually a way to persist and recover that organized data.</p>
<p>These days, when you say “Database” almost everyone thinks “Relational Database”, and SQL is the standard way to do that.</p>
<p>SQL RDBMS systems are robust, powerful, scalable, and very well optimized.</p>
<p>But: They require you to adapt the relational data model.</p>
</div>
<div class="section" id="non-rdbms-options">
<h3>Non RDBMS options:<a class="headerlink" href="#non-rdbms-options" title="Permalink to this headline"></a></h3>
<p>A key buzzword these days is “NOSQL”</p>
<p>OK: They don’t use SQL – but what are they?</p>
<p>Not one thing, but key features are mostly shared:</p>
<ul class="simple">
<li><p>“schema less”</p></li>
</ul>
<blockquote>
<div><ul class="simple">
<li><p>Document oriented</p></li>
</ul>
</div></blockquote>
<ul class="simple">
<li><p>More direct mapping to an object model.</p></li>
<li><p>Highly Scalable</p></li>
</ul>
<blockquote>
<div><ul class="simple">
<li><p>Easier to distribute / parallelize than RDBMSs</p></li>
</ul>
</div></blockquote>
</div>
<div class="section" id="database-schema">
<h3>Database Schema<a class="headerlink" href="#database-schema" title="Permalink to this headline"></a></h3>
<p><strong>Schema:</strong></p>
<p>A database schema is the organization of data, and description of how a database is constructed: Divided into database tables, and relationships: foreign keys, etc…</p>
<p>Includes what fields in which tables, what data type each field is, normalization of shared data, etc.</p>
<p>This requires a fair bit of work up-front, and can be hard to adapt as the system requirements changes.</p>
<p>It also can be a bit ugly to map your programming data model to the schema.</p>
</div>
<div class="section" id="schemaless">
<h3>Schemaless<a class="headerlink" href="#schemaless" title="Permalink to this headline"></a></h3>
<p>Schemaless databases generally follow a “document model”.</p>
<p>Each entry in the database is a “document”:</p>
<ul class="simple">
<li><p>essentially an arbitrary collection of fields.</p></li>
<li><p>often looks like a Python dict.</p></li>
</ul>
<p>Not every entry has to have exactly the same structure.</p>
<p>Maps well to dynamic programming languages.</p>
<p>Adapts well as the system changes.</p>
</div>
<div class="section" id="nosql-in-python">
<h3>NoSQL in Python:<a class="headerlink" href="#nosql-in-python" title="Permalink to this headline"></a></h3>
<p>Three Categories:</p>
</div>
<div class="section" id="simple-key-value-object-store">
<h3>1. Simple key-value object store:<a class="headerlink" href="#simple-key-value-object-store" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p>shelve</p></li>
<li><p>anydbm</p></li>
<li><p>Can store (almost) any Python object</p></li>
<li><p>Only provides storage and retrieval</p></li>
</ul>
</div>
<div class="section" id="external-nosql-system">
<h3>2. External NoSQL system:<a class="headerlink" href="#external-nosql-system" title="Permalink to this headline"></a></h3>
<ul>
<li><p>Python bindings to external NoSQL system</p></li>
<li><p>Doesn’t store full Python objects</p></li>
<li><p>Generally stores arbitrary collections of data (but not classes)</p></li>
<li><p>Can be simple key-value stores:</p>
<ul class="simple">
<li><p>Redis, etc…</p></li>
</ul>
</li>
<li><p>Or a more full featured document database:</p>
<ul class="simple">
<li><p>In-database searching, etc.</p></li>
<li><p>mongoDB, etc…</p></li>
</ul>
</li>
<li><p>“Graph” databases (<a class="reference internal" href="GraphDatabases.html#graph-databases"><span class="std std-ref">Graph Databases</span></a>):</p>
<ul class="simple">
<li><p>neo4j, etc.</p></li>
</ul>
</li>
<li><p>Or a Map/Reduce engine:</p>
<blockquote>
<div><ul class="simple">
<li><p>Hadoop</p></li>
</ul>
</div></blockquote>
</li>
</ul>
</div>
<div class="section" id="python-object-database">
<h3>3. Python Object Database:<a class="headerlink" href="#python-object-database" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p>Stores and retrieves arbitrary Python objects.</p>
<ul>
<li><p>Don’t need to adapt your data model at all.</p></li>
</ul>
</li>
<li><p>ZODB is the only robust maintained system (I know of)</p></li>
<li><p>ZODB is as close a match as you can get between the store and your code – references and everything.</p></li>
</ul>
<p><a class="reference external" href="http://blog.startifact.com/posts/older/a-misconception-about-the-zodb.html">http://blog.startifact.com/posts/older/a-misconception-about-the-zodb.html</a></p>
<p>(note that that post says “it’s been around for more than a decade”, and it was written a decade ago!)</p>
</div>
<div class="section" id="why-a-db-at-all">
<h3>Why a DB at all?<a class="headerlink" href="#why-a-db-at-all" title="Permalink to this headline"></a></h3>
<p>Reasons to use a database:</p>
<ul class="simple">
<li><p>Need to persist the data your application uses</p></li>
<li><p>May need to store more data than you can hold in memory</p></li>
<li><p>May need to have multiple applications (or multiple instances) accessing the same data</p></li>
<li><p>May need to scale – have the DB running on a separate server(s)</p></li>
<li><p>May need to access data from systems written in different languages.</p></li>
</ul>
</div>
<div class="section" id="zodb">
<h3>ZODB<a class="headerlink" href="#zodb" title="Permalink to this headline"></a></h3>
<p>The Zope Object Data Base: A native object database for Python</p>
<ul class="simple">
<li><p>Transparent persistence for Python objects</p></li>
<li><p>Full ACID-compatible transaction support (including savepoints)</p></li>
<li><p>History/undo ability</p></li>
<li><p>Efficient support for binary large objects (BLOBs)</p></li>
<li><p>Pluggable storages</p></li>
<li><p>Scalable architecture</p></li>
</ul>
<p><a class="reference external" href="http://www.zodb.org/">ZODB</a></p>
</div>
<div class="section" id="mongodb">
<h3>MongoDB<a class="headerlink" href="#mongodb" title="Permalink to this headline"></a></h3>
<p>Document-Oriented Storage</p>
<blockquote>
<div><ul class="simple">
<li><p>JSON-style documents with dynamic schemas offer simplicity and power.</p></li>
</ul>
</div></blockquote>
<dl class="simple">
<dt>Full Index Support</dt><dd><ul class="simple">
<li><p>Index on any attribute, just like you’re used to.</p></li>
</ul>
</dd>
<dt>Replication & High Availability</dt><dd><ul class="simple">
<li><p>Mirror across LANs and WANs for scale and peace of mind.</p></li>
</ul>
</dd>
<dt>Auto-Sharding</dt><dd><ul class="simple">
<li><p>Scale horizontally without compromising functionality.</p></li>
</ul>
</dd>
<dt>Querying</dt><dd><ul class="simple">
<li><p>Rich, document-based queries.</p></li>
</ul>
</dd>
</dl>
<p><a class="reference external" href="https://www.mongodb.org/">MongoDB Web Site</a></p>
</div>
<div class="section" id="other-options-to-consider">
<h3>Other Options to Consider:<a class="headerlink" href="#other-options-to-consider" title="Permalink to this headline"></a></h3>
<p>Redis: Advanced, Scalable key-value store.
( not well supported on Windows :-( )</p>
<ul class="simple">
<li><p><a class="reference external" href="http://redis.io/">http://redis.io/</a></p></li>
</ul>
<p>Riak: High availablity/scalablity (but not so good for small)</p>
<ul class="simple">
<li><p><a class="reference external" href="http://docs.basho.com/riak/latest/dev/taste-of-riak/python/">http://docs.basho.com/riak/latest/dev/taste-of-riak/python/</a></p></li>
</ul>
<p>HyperDex: “Next generation key-value store”</p>
<ul class="simple">
<li><p><a class="reference external" href="http://hyperdex.org/">http://hyperdex.org/</a></p></li>
</ul>
<p>Apache Cassandra: A more schema-based NoSQL solution</p>
<ul class="simple">
<li><p><a class="reference external" href="http://pycassa.github.io/pycassa/">http://pycassa.github.io/pycassa/</a></p></li>
</ul>
<p>This is a nice page with a summary:</p>
<ul class="simple">
<li><p><a class="reference external" href="https://www.fullstackpython.com/no-sql-datastore.html">https://www.fullstackpython.com/no-sql-datastore.html</a></p></li>
</ul>
<p>(there are some good links to other resources on that page, too)</p>
</div>
</div>
<div class="section" id="an-example">
<h2>An Example<a class="headerlink" href="#an-example" title="Permalink to this headline"></a></h2>
<p>The following are examples of using some of these systems to store some data.</p>
<div class="section" id="the-data-model">
<h3>The Data Model<a class="headerlink" href="#the-data-model" title="Permalink to this headline"></a></h3>
<p>To store your data, you need to have a structure for the data – this is the data model. For this example, we will build an Address Book with a not quite trivial data model.</p>
<p>I’m a programmer first, and a database guy second (or third or…) so I start with the data model I want in the code.</p>
<p>There are people:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="bp">self</span><span class="o">.</span><span class="n">first_name</span>
<span class="bp">self</span><span class="o">.</span><span class="n">last_name</span>
<span class="bp">self</span><span class="o">.</span><span class="n">middle_name</span>
<span class="bp">self</span><span class="o">.</span><span class="n">cell_phone</span>
<span class="bp">self</span><span class="o">.</span><span class="n">email</span>
</pre></div>
</div>
<p>There are households:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="bp">self</span><span class="o">.</span><span class="n">name</span>
<span class="bp">self</span><span class="o">.</span><span class="n">people</span>
<span class="bp">self</span><span class="o">.</span><span class="n">address</span>
<span class="bp">self</span><span class="o">.</span><span class="n">phone</span>
</pre></div>
</div>
<p>(similarly businesses)</p>
<p><a class="reference download internal" download="" href="../_downloads/409972d036b41f742f82ab8275e77818/address_book_model.py"><code class="xref download docutils literal notranslate"><span class="pre">address_book_model.py</span></code></a></p>
</div>
<div class="section" id="using-zodb">
<h3>Using ZODB<a class="headerlink" href="#using-zodb" title="Permalink to this headline"></a></h3>
<p>ZODB stored Python objects.</p>
<p>To make an object persistent (persistent should be installed with zodb):</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">persistent</span>
<span class="k">class</span> <span class="nc">Something</span><span class="p">(</span><span class="n">persistent</span><span class="o">.</span><span class="n">Persistent</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">a_field</span> <span class="o">=</span> <span class="s1">''</span>
<span class="bp">self</span><span class="o">.</span><span class="n">another_field</span> <span class="s1">''</span>
</pre></div>
</div>
<p>When a change is made to the fields, the DB will keep it updated.</p>
</div>
<div class="section" id="mutable-attributes">
<h3>Mutable Attributes<a class="headerlink" href="#mutable-attributes" title="Permalink to this headline"></a></h3>
<p><code class="docutils literal notranslate"><span class="pre">Something.this</span> <span class="pre">=</span> <span class="pre">that</span></code> will trigger a DB action</p>
<p>But:</p>
<p><code class="docutils literal notranslate"><span class="pre">Something.a_list.append</span></code> will not trigger anything.</p>
<p>The DB doesn’t know that that the list has been altered.</p>
<p>Solution:</p>
<p><code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">persistent.list</span> <span class="pre">import</span> <span class="pre">PersistentList</span></code></p>
<p><code class="docutils literal notranslate"><span class="pre">self.a_list</span> <span class="pre">=</span> <span class="pre">PersistentList()</span></code></p>
<p>(also <code class="docutils literal notranslate"><span class="pre">PersistantDict()</span></code> )</p>
<p>(or write getters and setters…)</p>
<p><code class="docutils literal notranslate"><span class="pre">Examples/nosql/address_book_zodb.py</span></code></p>
</div>
<div class="section" id="id2">
<h3>mongoDB<a class="headerlink" href="#id2" title="Permalink to this headline"></a></h3>
<p>Essentially a key-value store, but the values are JSON-like objects.
(Actually BSON (binary JSON) )</p>
<dl class="simple">
<dt>So you can store any object that can look like JSON:</dt><dd><ul class="simple">
<li><p>dicts</p></li>
<li><p>lists</p></li>
<li><p>numbers</p></li>
<li><p>strings</p></li>
<li><p>richer than JSON.</p></li>
</ul>
</dd>
</dl>
</div>
<div class="section" id="mongodb-and-python">
<h3>mongoDB and Python<a class="headerlink" href="#mongodb-and-python" title="Permalink to this headline"></a></h3>
<p>mongoDB is written in C++ – can be accessed by various language drivers.</p>
<p><a class="reference external" href="http://docs.mongodb.org/manual/applications/drivers/">http://docs.mongodb.org/manual/applications/drivers/</a></p>
<p>For Python: <code class="docutils literal notranslate"><span class="pre">PyMongo</span></code></p>
<p><a class="reference external" href="http://api.mongodb.org/python/current/tutorial.html">http://api.mongodb.org/python/current/tutorial.html</a></p>
<p>To install the python api for mongoDB:</p>
<p><code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">pymongo</span></code> - binary wheels available!</p>
<p>There are also various tools for integrating mongoDB with Frameworks:</p>
<ul class="simple">
<li><p>Django MongoDB Engine</p></li>
<li><p>mongodb_beaker</p></li>
<li><p>MongoLog: Python logging handler</p></li>
<li><p>Flask-PyMongo</p></li>
<li><p>others…</p></li>
</ul>
</div>
<div class="section" id="getting-started-with-mongodb">
<h3>Getting started with mongoDB<a class="headerlink" href="#getting-started-with-mongodb" title="Permalink to this headline"></a></h3>
<p>The mongoDB (database) is a separate program. Installers here:</p>
<p><a class="reference external" href="http://www.mongodb.org/downloads">http://www.mongodb.org/downloads</a></p>
<p><strong>NOTE:</strong> mongo is also available as a service, with a free “sandbox” to try it out:</p>
<p><a class="reference external" href="https://www.mongodb.com/cloud/atlas">https://www.mongodb.com/cloud/atlas</a></p>
<div class="section" id="installing-mongo">
<h4>Installing Mongo<a class="headerlink" href="#installing-mongo" title="Permalink to this headline"></a></h4>
<p>Simple copy and paste install or use homebrew (at least on OS-X)</p>
<p>Drop the files from <code class="docutils literal notranslate"><span class="pre">bin</span></code> into <code class="docutils literal notranslate"><span class="pre">usr/local/bin</span></code> or similar, or in your home dir somewhere you can find them.</p>
<ul class="simple">
<li><p>I put it in a “mongo” dir in my home dir. Then added it to my PATH for now:</p>
<ul>
<li><p>Editing <code class="docutils literal notranslate"><span class="pre">~/.bash_profile</span></code>, and adding:</p></li>
</ul>
</li>
</ul>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Adding PATH for mongo local install</span>
<span class="n">PATH</span><span class="o">=</span><span class="s2">"~/mongo/bin:$</span><span class="si">{PATH}</span>
<span class="n">export</span> <span class="n">PATH</span>
</pre></div>
</div>
</div>
<div class="section" id="anaconda-install">
<h4>Anaconda Install<a class="headerlink" href="#anaconda-install" title="Permalink to this headline"></a></h4>
<p>If you are using the Anaconda Python distribution (or miniconda) Mongo is available from conda:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">conda</span> <span class="n">install</span> <span class="n">mongodb</span> <span class="n">pymongo</span>
</pre></div>
</div>
</div>
<div class="section" id="starting-mongo">
<h4>Starting Mongo<a class="headerlink" href="#starting-mongo" title="Permalink to this headline"></a></h4>
<p>Create a dir for the database:</p>
<p><code class="docutils literal notranslate"><span class="pre">$</span> <span class="pre">mkdir</span> <span class="pre">mongo_data</span></code></p>
<p>And start it up:</p>
<p><code class="docutils literal notranslate"><span class="pre">$</span> <span class="pre">mongod</span> <span class="pre">--dbpath=mongo_data/</span></code></p>
<p>It will give you a bunch of startup messages, and then end by indicating which port it is listening on:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">I</span> <span class="n">NETWORK</span> <span class="p">[</span><span class="n">initandlisten</span><span class="p">]</span> <span class="n">waiting</span> <span class="k">for</span> <span class="n">connections</span> <span class="n">on</span> <span class="n">port</span> <span class="mi">27017</span>
</pre></div>
</div>
<p>So you know you can connect to it on port 27017</p>
</div>
</div>
<div class="section" id="creating-a-db">
<h3>Creating a DB:<a class="headerlink" href="#creating-a-db" title="Permalink to this headline"></a></h3>
<p>Make sure you’ve got the mongo drivers installed:</p>
<p>pip install pymongo</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># create the DB</span>
<span class="kn">from</span> <span class="nn">pymongo</span> <span class="kn">import</span> <span class="n">MongoClient</span>
<span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">,</span> <span class="mi">27017</span><span class="p">)</span>
<span class="n">store</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">store_name</span> <span class="c1"># creates a Database</span>
<span class="n">people</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">people</span> <span class="c1"># creates a collection</span>
</pre></div>
</div>
<p>Mongo will link to the given database and collection, or create new ones if they don’t exist.</p>
<p>Adding some stuff:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">people</span><span class="o">.</span><span class="n">insert_one</span><span class="p">({</span><span class="s1">'first_name'</span><span class="p">:</span> <span class="s1">'Fred'</span><span class="p">,</span>
<span class="s1">'last_name'</span><span class="p">:</span> <span class="s1">'Jones'</span><span class="p">})</span>
</pre></div>
</div>
</div>
<div class="section" id="pulling-stuff-out">
<h3>Pulling Stuff Out:<a class="headerlink" href="#pulling-stuff-out" title="Permalink to this headline"></a></h3>
<p>And reading it back:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [16]: </span><span class="n">people</span><span class="o">.</span><span class="n">find_one</span><span class="p">({</span><span class="s1">'first_name'</span><span class="p">:</span><span class="s2">"Fred"</span><span class="p">})</span>
<span class="gh">Out[16]:</span>
<span class="go"> {'_id': ObjectId('534dcdcb5c84d28b596ad15e'),</span>
<span class="go"> 'first_name': 'Fred',</span>
<span class="go"> 'last_name': 'Jones'}</span>
</pre></div>
</div>
<p>Note that it adds an ObjectID for you.</p>
<p><a class="reference download internal" download="" href="../_downloads/5aa816096a0d060405a1f5d2c6bce87f/address_book_mongo.py"><code class="xref download docutils literal notranslate"><span class="pre">/examples/nosql/address_book_mongo.py</span></code></a></p>
<p>and</p>
<p><a class="reference download internal" download="" href="../_downloads/37f2d0abd497b27b3fed8a8b1d89d6fc/test_address_book_mongo.py"><code class="xref download docutils literal notranslate"><span class="pre">/examples/nosql/test_address_book_mongo.py</span></code></a></p>
<p>(or in the class repo in : <code class="docutils literal notranslate"><span class="pre">examples/nosql</span></code>)</p>
</div>
</div>
</div>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="Debugging.html" class="btn btn-neutral float-left" title="Debugging" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="GraphDatabases.html" class="btn btn-neutral float-right" title="Graph Databases" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>© Copyright 2020, University of Washington, Natasha Aleksandrova, Christopher Barker, Brian Dorsey, Cris Ewing, Christy Heaton, Jon Jacky, Maria McKinley, Andy Miles, Rick Riehle, Joseph Schilz, Joseph Sheedy, Hosung Song. Creative Commons Attribution-ShareAlike 4.0 license.</p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>