MongoDB - First try
Installation steps¶
Run the following commands to install MongoDB.
jitsejan@vps:/$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
jitsejan@jjvps:/$ echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
jitsejan@jjvps:/$ sudo apt-get update
jitsejan@jjvps:/$ sudo apt-get install -y mongodb-org
Start MongoDB¶
jitsejan@jjvps:/$ sudo service mongod start
In [1]:
!tail /var/log/mongodb/mongod.log
In [2]:
!pip install pymongo
Connect to MongoDB¶
In [3]:
import pymongo
print(pymongo.version)
client = pymongo.MongoClient('mongodb://localhost:27017/')
Check which databases already exist¶
In [4]:
client.database_names()
Out[4]:
Create a new database¶
You can create a database by simply selecting the non-existing database. Only when a document is written, the database will physically be created.
In [5]:
db = client.nintendo_db
db
Out[5]:
Create a new collection¶
In [6]:
characters = db.characters
characters
Out[6]:
Create documents¶
For simplicity, I will use the data that I have used in another notebook for creating documents.
In [7]:
import pandas as pd
character_df = pd.read_csv('../data/nintendo_characters.csv')
character_df
Out[7]:
In [8]:
import json
characters_dict = character_df.to_dict(orient='records')
print(json.dumps(characters_dict[0], indent=4))
In [9]:
for character in characters_dict:
character_id = characters.insert_one(character).inserted_id
print(character_id)
Verify the new collection has been created¶
In [10]:
db.collection_names(include_system_collections=False)
Out[10]:
Verify the characters have been added¶
Check the number of documents for the characters collection.
In [11]:
characters.count()
Out[11]:
Check if Luigi is in the database.
In [12]:
characters.find_one({"name": "Luigi"})
Out[12]:
Retrieve all documents in the characters collection¶
In [13]:
characters_from_db = list(characters.find({}))
characters_from_db[0]
Out[13]:
Find the red characters¶
Only retrieve the name and description of the character.
In [14]:
red_characters = list(characters.find({"color": "red"}, {"name":1, "description":1, "_id":0}))
red_characters
Out[14]:
Create a dataframe from the results¶
In [15]:
import pandas as pd
red_characters_df = pd.DataFrame.from_dict(red_characters)
red_characters_df
Out[15]:
Drop the database¶
In [16]:
client.drop_database('nintendo_db')