Last active
April 26, 2018 06:45
-
-
Save lkluft/7cd2e416b3e08a569caf8f7b87dfa31e to your computer and use it in GitHub Desktop.
First steps with MongoDB and pymongo.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "from pymongo import MongoClient\nfrom random import (randint, choice, seed)\n\n\nseed(1234) # make random reproducible\n\n# Connect to MongoDB instance running on localhost.\nclient = MongoClient(username='lkluft', password='xxx') \ndb = client.testdb # Open test database.\n\nnames = ['Foo', 'Bar', 'Baz']\nitems = [{'name': choice(names),\n 'age': randint(10, 60),\n 'score': randint(0, 10)}\n for i in range(100)]\n\n# If the collection is empty...\nif db.scoreboard.count() == 0:\n # ... insert documents to collection.\n db.scoreboard.insert_many(items) \n\n# Print all documents with a score higher than 7 sorted by age.\nfor entry in db.scoreboard.find({'score': {'$gt': 7}}).sort('age'):\n print(entry)", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "{'_id': ObjectId('5ae174ef30b0d48d6d4cb3eb'), 'name': 'Foo', 'age': 10, 'score': 10}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb3ca'), 'name': 'Bar', 'age': 12, 'score': 9}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb3ec'), 'name': 'Baz', 'age': 13, 'score': 10}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb3b5'), 'name': 'Foo', 'age': 14, 'score': 10}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb3c1'), 'name': 'Foo', 'age': 14, 'score': 9}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb409'), 'name': 'Foo', 'age': 18, 'score': 9}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb3c5'), 'name': 'Baz', 'age': 19, 'score': 10}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb3c7'), 'name': 'Bar', 'age': 27, 'score': 10}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb3f6'), 'name': 'Baz', 'age': 27, 'score': 8}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb401'), 'name': 'Foo', 'age': 28, 'score': 9}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb3cc'), 'name': 'Bar', 'age': 31, 'score': 9}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb40c'), 'name': 'Baz', 'age': 31, 'score': 9}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb3b0'), 'name': 'Foo', 'age': 32, 'score': 10}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb3e1'), 'name': 'Baz', 'age': 39, 'score': 8}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb3b1'), 'name': 'Baz', 'age': 40, 'score': 9}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb405'), 'name': 'Baz', 'age': 40, 'score': 10}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb3f1'), 'name': 'Bar', 'age': 44, 'score': 10}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb402'), 'name': 'Bar', 'age': 51, 'score': 9}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb40b'), 'name': 'Bar', 'age': 53, 'score': 10}\n{'_id': ObjectId('5ae174ef30b0d48d6d4cb3cf'), 'name': 'Bar', 'age': 56, 'score': 8}\n", | |
"name": "stdout" | |
} | |
] | |
} | |
], | |
"metadata": { | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/7cd2e416b3e08a569caf8f7b87dfa31e" | |
}, | |
"gist": { | |
"id": "7cd2e416b3e08a569caf8f7b87dfa31e", | |
"data": { | |
"description": "First steps with MongoDB and pymongo.", | |
"public": true | |
} | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.6.5", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment