Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MongoDB Notebook, find it here: http://nbviewer.ipython.org/gist/ErikBjare/11092794
{
"metadata": {
"name": "MongoDB Notebook"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from datetime import datetime, timedelta\n",
"\n",
"import pymongo\n",
"from pymongo import MongoClient"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 48
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"client = MongoClient(\"localhost\", 27017)\n",
"db = client.test_database"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"db.create_collection(\"test\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 9,
"text": [
"Collection(Database(MongoClient('localhost', 27017), 'test_database'), 'test')"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"db.collection_names()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 10,
"text": [
"['system.indexes', 'test']"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"db.test.insert({\"users\": [{\"name\": \"Erik Bj\u00e4reholt\", \"nick\": \"erb\"}]})\n",
"db.test.find_one({\"users\": [{\"name\": \"Erik Bj\u00e4reholt\", \"nick\": \"erb\"}]})"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 29,
"text": [
"{'_id': ObjectId('5352b3e8a8a823033d84be6a'),\n",
" 'users': [{'name': 'Erik Bj\u00e4reholt', 'nick': 'erb'}]}"
]
}
],
"prompt_number": 29
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"db.drop_collection(\"test\")\n",
"db.collection_names()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 39,
"text": [
"['system.indexes', 'lifelogger-sheet']"
]
}
],
"prompt_number": 39
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"db.drop_collection(\"lifelogger-users\")\n",
"db.drop_collection(\"lifelogger-sheets\")"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 40
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"users = db.create_collection(\"lifelogger-users\")\n",
"sheets = db.create_collection(\"lifelogger-sheets\")"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 41
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"user_erb = users.insert({\"username\": \"erb\", \"name\": \"Erik Bj\u00e4reholt\", \"email\": \"erik.bjareholt@gmail.com\"})\n",
"user_tester = users.insert({\"username\": \"tester\", \"name\": \"Test McTest\", \"email\": \"tester@example.com\"})"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 45
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"user_erb = users.find_one({\"username\": \"erb\"})"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 65
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class Sheet():\n",
" \"\"\"Baseclass for all logging-sheets\"\"\"\n",
" \n",
" def __init__(self, user, year=None, month=None):\n",
" documents = sheets.find({\"username\": user[\"username\"]})\n",
" if year and month:\n",
" d = datetime(year, month, 1)\n",
" elif year or month:\n",
" raise ValueError(\"You must specify both year and month, or neither.\")\n",
" else:\n",
" d = datetime.now()\n",
" found = False\n",
" for document in documents:\n",
" if document and document[\"year\"] == d.year and document[\"month\"] == d.month:\n",
" found = True\n",
" break\n",
" if not found:\n",
" document = {\"username\": user[\"username\"],\n",
" \"columns\": {\"mood\": {\"08:00\": [], \"12:00\": [], \"20:00\": []},\n",
" \"productivity\": {\"08:00\": [], \"12:00\": [], \"20:00\": []},\n",
" \"exercise\": {\"running\": [], \"gym\": [], \"home\": []}},\n",
" \"year\": d.year,\n",
" \"month\": d.month}\n",
" sheets.insert(document)\n",
" self.data = document\n",
" \n",
" def __repr__(self):\n",
" return str(self.data)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 104
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sheet_erb = Sheet(user_erb)\n",
"sheet_erb_2013_1 = Sheet(user_erb, year=2013, month=1)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 105
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for sheet in sheets.find():\n",
" print(sheet)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{'columns': {'productivity': {'08:00': [], '20:00': [], '12:00': []}, 'exercise': {'running': [], 'gym': [], 'home': []}, 'mood': {'08:00': [], '20:00': [], '12:00': []}}, 'year': 2014, 'username': 'erb', '_id': ObjectId('5352bb47a8a823033d84be73'), 'month': 4}\n",
"{'columns': {'productivity': {'08:00': [], '20:00': [], '12:00': []}, 'exercise': {'running': [], 'gym': [], 'home': []}, 'mood': {'08:00': [], '20:00': [], '12:00': []}}, 'year': 2013, 'username': 'erb', '_id': ObjectId('5352bcbca8a823033d84be74'), 'month': 1}\n"
]
}
],
"prompt_number": 106
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment