Skip to content

Instantly share code, notes, and snippets.

@gwbischof
Created November 9, 2018 22:05
Show Gist options
  • Save gwbischof/388ad173925f8c2603188b9ab4c2622b to your computer and use it in GitHub Desktop.
Save gwbischof/388ad173925f8c2603188b9ab4c2622b to your computer and use it in GitHub Desktop.
Really easy way to get a mongodb running, with demonstration of a few PyMongo read/write methods.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Mongo in a sandbox\n",
"- Very easy to get database running"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: pymongo in /home/gbischof/miniconda3/envs/mongo/lib/python3.6/site-packages (3.7.2)\n",
"Requirement already satisfied: mongobox in /home/gbischof/miniconda3/envs/mongo/lib/python3.6/site-packages (0.1.8)\n"
]
}
],
"source": [
"# Official python mongo library\n",
"!pip install pymongo\n",
"!pip install mongobox"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from mongobox import MongoBox\n",
"\n",
"box = MongoBox()\n",
"box.start()\n",
"\n",
"from pymongo import MongoClient\n",
"client = box.client()\n",
"\n",
"# Other contructor calls\n",
"#client = MongoClient('localhost', 27017)\n",
"#client = MongoClient('mongodb://localhost:27017')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"#To specify which database you actually want to use, you can access it as an attribute.\n",
"#By specifying this database name and saving data to it, you create the database automatically.\n",
"\n",
"db = client.pymongo_test\n",
"# db = client['pymongo_test']"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"One post: 5be604a1ed513d10e135a52b\n"
]
}
],
"source": [
"# Inserting 1 document\n",
"\n",
"posts = db.posts\n",
"\n",
"post_data = {\n",
" 'title': 'Python and MongoDB',\n",
" 'content': 'PyMongo is fun, you guys',\n",
" 'author': 'Scott'\n",
"}\n",
"\n",
"result = posts.insert_one(post_data)\n",
"\n",
"print('One post: {0}'.format(result.inserted_id))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Multiple posts: [ObjectId('5be604a7ed513d10e135a52c'), ObjectId('5be604a7ed513d10e135a52d'), ObjectId('5be604a7ed513d10e135a52e')]\n"
]
}
],
"source": [
"# Inserting multiple documents\n",
"post_1 = {\n",
" 'title': 'Python and MongoDB',\n",
" 'content': 'PyMongo is fun, you guys',\n",
" 'author': 'Scott'\n",
"}\n",
"post_2 = {\n",
" 'title': 'Virtual Environments',\n",
" 'content': 'Use virtual environments, you guys',\n",
" 'author': 'Scott'\n",
"}\n",
"post_3 = {\n",
" 'title': 'Learning Python',\n",
" 'content': 'Learn Python, it is easy',\n",
" 'author': 'Bill'\n",
"}\n",
"new_result = posts.insert_many([post_1, post_2, post_3])\n",
"print('Multiple posts: {0}'.format(new_result.inserted_ids))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'_id': ObjectId('5be604a7ed513d10e135a52c'), 'title': 'Python and MongoDB', 'content': 'PyMongo is fun, you guys', 'author': 'Scott'}\n"
]
}
],
"source": [
"# Finding one document\n",
"\n",
"bills_post = posts.find_one({'author': 'Scott'})\n",
"print(bills_post)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'_id': ObjectId('5be604a7ed513d10e135a52c'), 'title': 'Python and MongoDB', 'content': 'PyMongo is fun, you guys', 'author': 'Scott'}\n",
"{'_id': ObjectId('5be604a7ed513d10e135a52d'), 'title': 'Virtual Environments', 'content': 'Use virtual environments, you guys', 'author': 'Scott'}\n"
]
}
],
"source": [
"# Find many documents\n",
"\n",
"scotts_posts = posts.find({'author': 'Scott'})\n",
"for post in scotts_posts:\n",
" print(post)\n",
"\n",
"#print(list(scotts_posts))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Mongo in a sandbox\n",
"- Very easy to get database running"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: pymongo in /home/gbischof/miniconda3/envs/mongo/lib/python3.6/site-packages (3.7.2)\n",
"Requirement already satisfied: mongobox in /home/gbischof/miniconda3/envs/mongo/lib/python3.6/site-packages (0.1.8)\n"
]
}
],
"source": [
"# Official python mongo library\n",
"!pip install pymongo\n",
"!pip install mongobox"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from mongobox import MongoBox\n",
"\n",
"box = MongoBox()\n",
"box.start()\n",
"\n",
"from pymongo import MongoClient\n",
"client = box.client()\n",
"\n",
"# Other contructor calls\n",
"#client = MongoClient('localhost', 27017)\n",
"#client = MongoClient('mongodb://localhost:27017')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"#To specify which database you actually want to use, you can access it as an attribute.\n",
"#By specifying this database name and saving data to it, you create the database automatically.\n",
"\n",
"db = client.pymongo_test\n",
"# db = client['pymongo_test']"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"One post: 5be604a1ed513d10e135a52b\n"
]
}
],
"source": [
"# Inserting 1 document\n",
"\n",
"posts = db.posts\n",
"\n",
"post_data = {\n",
" 'title': 'Python and MongoDB',\n",
" 'content': 'PyMongo is fun, you guys',\n",
" 'author': 'Scott'\n",
"}\n",
"\n",
"result = posts.insert_one(post_data)\n",
"\n",
"print('One post: {0}'.format(result.inserted_id))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Multiple posts: [ObjectId('5be604a7ed513d10e135a52c'), ObjectId('5be604a7ed513d10e135a52d'), ObjectId('5be604a7ed513d10e135a52e')]\n"
]
}
],
"source": [
"# Inserting multiple documents\n",
"post_1 = {\n",
" 'title': 'Python and MongoDB',\n",
" 'content': 'PyMongo is fun, you guys',\n",
" 'author': 'Scott'\n",
"}\n",
"post_2 = {\n",
" 'title': 'Virtual Environments',\n",
" 'content': 'Use virtual environments, you guys',\n",
" 'author': 'Scott'\n",
"}\n",
"post_3 = {\n",
" 'title': 'Learning Python',\n",
" 'content': 'Learn Python, it is easy',\n",
" 'author': 'Bill'\n",
"}\n",
"new_result = posts.insert_many([post_1, post_2, post_3])\n",
"print('Multiple posts: {0}'.format(new_result.inserted_ids))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'_id': ObjectId('5be604a7ed513d10e135a52c'), 'title': 'Python and MongoDB', 'content': 'PyMongo is fun, you guys', 'author': 'Scott'}\n"
]
}
],
"source": [
"# Finding one document\n",
"\n",
"bills_post = posts.find_one({'author': 'Scott'})\n",
"print(bills_post)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'_id': ObjectId('5be604a7ed513d10e135a52c'), 'title': 'Python and MongoDB', 'content': 'PyMongo is fun, you guys', 'author': 'Scott'}\n",
"{'_id': ObjectId('5be604a7ed513d10e135a52d'), 'title': 'Virtual Environments', 'content': 'Use virtual environments, you guys', 'author': 'Scott'}\n"
]
}
],
"source": [
"# Find many documents\n",
"\n",
"scotts_posts = posts.find({'author': 'Scott'})\n",
"for post in scotts_posts:\n",
" print(post)\n",
"\n",
"#print(list(scotts_posts))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment