Skip to content

Instantly share code, notes, and snippets.

@harisn5181
Forked from nikhilkumarsingh/mongodb_atlas.ipynb
Created March 20, 2022 19:46
Show Gist options
  • Select an option

  • Save harisn5181/65dc2c2deb9665ec7302417ce759408f to your computer and use it in GitHub Desktop.

Select an option

Save harisn5181/65dc2c2deb9665ec7302417ce759408f to your computer and use it in GitHub Desktop.
Working with mongoDB Atlas using Python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Getting started with MongoDB Atlas\n",
"\n",
"https://www.mongodb.com/cloud/atlas\n",
"\n",
"![](https://cdn-images-1.medium.com/max/907/1*02LVdlAj9fhWpu36LIKWmw.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"pip install pymongo\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from pymongo import MongoClient"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"client = MongoClient(\"mongodb+srv://test:[email protected]/test?retryWrites=true\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"db = client.get_database('student_db')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"records = db.student_records"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Count documents"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"records.count_documents({})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create new document"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"new_student = {\n",
" 'name': 'ram',\n",
" 'roll_no': 321,\n",
" 'branch': 'it'\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<pymongo.results.InsertOneResult at 0x7f8775ade308>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"records.insert_one(new_student)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"new_students = [\n",
" {\n",
" 'name': 'alex',\n",
" 'roll_no': 320,\n",
" 'branch': 'it'\n",
" },\n",
" {\n",
" 'name': 'john',\n",
" 'roll_no': 30,\n",
" 'branch': 'ece'\n",
" }\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<pymongo.results.InsertManyResult at 0x7f8775ad3488>"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"records.insert_many(new_students)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Find documents"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'_id': ObjectId('5caa28c7faaa4630713346ae'),\n",
" 'name': 'ram',\n",
" 'roll_no': 321,\n",
" 'branch': 'it'},\n",
" {'_id': ObjectId('5caa28c8faaa4630713346af'),\n",
" 'name': 'alex',\n",
" 'roll_no': 320,\n",
" 'branch': 'it'},\n",
" {'_id': ObjectId('5caa28c8faaa4630713346b0'),\n",
" 'name': 'john',\n",
" 'roll_no': 30,\n",
" 'branch': 'ece'}]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(records.find())"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"records.find_one({'roll_no': 123})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Update documents"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"student_updates = {\n",
" 'name': 'Nikhil'\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<pymongo.results.UpdateResult at 0x7f8775ad3848>"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"records.update_one({'roll_no': 123}, {'$set': student_updates})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Delete documents"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<pymongo.results.DeleteResult at 0x7f87752780c8>"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"records.delete_one({'roll_no': 123})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"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