Opening convocation
MongoDB from Python
http://api.mongodb.org/python/current/api/
http://api.mongodb.com/python/current/tutorial.html
https://www.mongodb.com/json-and-bson
School of Information Studies | Syracuse University
1
SQL vs NoSQL
SQL NoSQL
Database Database
Table Collection
Tuple/row Document
Column Field
Table join Embedded document
Primary key Primary key
School of Information Studies | Syracuse University
2
Installation
conda install pymongo
conda install mongodb
**ONLY USE BELOW IF NEEDED**
pip install pymongo –user
pip install mongodb –user
School of Information Studies | Syracuse University
3
Using MongoDB with Python
start the server in one command prompt
>>mongod
**some windows users MAY need to navigate to mongo bin directory**
**NEED DB PATH**
Option 1) create folder structure in default path
Option 2) >>mongod –dbpath *path*
Once working – open 2nd prompt window to start server
>>mongo
Use ctrl+c to close both prompt windows
School of Information Studies | Syracuse University
4
Confirming installation and interacting with databases
> show dbs
> use peopledb
> db
School of Information Studies | Syracuse University
5
{“people”:[
{ “name”: “John Smith”, “age”: 30 }
{ “name”: “Bo Bennett”, “age”: 23 }
{ “name”: “Anna Jones”, “age”: 25 }
]}
School of Information Studies | Syracuse University
6
> db.people.insert({ “name”: “John Smith”, “age”: 30 })
> db.people.insert([{ “name”: “Bo Bennett”, “age”: 23 },{ “name”: “Anna Jones”, “age”: 25 }])
> show collections
> db.people.find()
> db.people.find().pretty()
> db.dropDatabase()
School of Information Studies | Syracuse University
7
Using pymongo
>>> import pymongo
Be sure that mongo server is started
>>> from pymongo import MongoClient
>>> client = MongoClient(‘localhost’, 27017)
>>> client.database_names()
>>> db = client.peopledb
>>> db.collection_names()
>>> peoplecoll = db.people
>>> type(peoplecoll)
School of Information Studies | Syracuse University
8
Creating a collection and adding
>>> peoplelist = [{ “name”: “John Smith”, “age”: 30 }, { “name”: “Bo Bennett”, “age”: 23 }, { “name”: “Anna Jones”, “age”: 25 }]
>>> peoplecoll.insert({ “name”: “John Smith”, “age”: 30 })
ObjectId(’58cfeeb664a4f302cd1bcb1c’)
>>> peoplecoll.insert_many(peoplelist[1: ])
>>> docs = peoplecoll.find()
>>> type(docs)
>>> for doc in docs:
… print(doc)
…
School of Information Studies | Syracuse University
9
Adding to the collection
>>> morepeoplelist = [{ “name”: “Britney Sykes”, “age”: 21 , ‘position’:’Guard’}, { “name”: “Briana Day”, “age”: 21, ‘position’:’Center’}, { “name”: “Alexis Peterson”, “age”: 21, ‘position’:’Guard’ }, { “name”: “Gabby Cooper”, “age”: 18, ‘position’:’Guard’}]
>>> peoplecoll.insert(morepeoplelist)
>>> docs = peoplecoll.find()
>>> for doc in docs:
… print(doc)
…
School of Information Studies | Syracuse University
10
Querying the collection
>>> peoplecoll.find_one()
>>> result = peoplecoll.find_one({‘name’:’Anna Jones’})
>>> print(result)
>>> results = peoplecoll.find({‘position’:’Guard’})
>>> for result in results:
… print(result)
…
School of Information Studies | Syracuse University
11
>>> from bson.objectid import ObjectId
>>> pid = ObjectId(’58cff3e164a4f302cd1bcb20′)
>>> result = peoplecoll.find_one({‘_id’:pid})
>>> print(result)
>>> number = peoplecoll.count({‘position’:’Guard’})
>>> number
School of Information Studies | Syracuse University
12
>>> results = peoplecoll.find({‘age’: {‘$lt’: 25}})
>>> for res in results:
… print(res)
…
>>> results = peoplecoll.find({‘age’: {‘$gt’: 20}}).sort(‘name’)
>>> for res in results:
… print(res)
…
School of Information Studies | Syracuse University
13
Getting JSON and putting it into Mongo
>>> client.database_names()
>>> eqdb = client.usgs
>>> quakecoll = eqdb.earthquakes
>>> docs = quakecoll.find()
>>> for doc in docs:
… print(doc[‘properties’][‘title’])
…
>>> eqdb.drop_collection(‘earthquakes’)
>>> eqdb.collection_names()
School of Information Studies | Syracuse University
14
/docProps/thumbnail.jpeg