Skip to content

Instantly share code, notes, and snippets.

@stevemclaugh
Last active November 18, 2019 05:39
Show Gist options
  • Save stevemclaugh/1fac225412d6c9a5c74bff481100e5f1 to your computer and use it in GitHub Desktop.
Save stevemclaugh/1fac225412d6c9a5c74bff481100e5f1 to your computer and use it in GitHub Desktop.

Install MongoDB and Python wrapper (Ubuntu Linux)

sudo apt-get install -y mongodb-org
pip install pymongo
pip3 install pymongo

Start MongoDB daemon

mongod

#or:
#service mongod start

Load records into DB

mongoimport --db db_name_here --collection collection_name_here --file /path/to/BSON_file.bson

Initialize MongoDB in Python

from pymongo import MongoClient
from pprint import pprint

client = MongoClient()

records = client.db_name_here.collection_name_here

Search for exact creator name

cursor = records.find({ 'pbcoreDescriptionDocument.pbcoreCreator.creator' :  "Worden, Michelle"})

for item in cursor:
    print(item)

Search for exact program title

cursor = records.find({ 'pbcoreDescriptionDocument.pbcoreTitle.#text' :  "Grace: A Tribute to Pope John Paul II"})

for item in cursor:
    print(item)

Search for records containing "Music" as a genre

cursor = records.find({ 'pbcoreDescriptionDocument.pbcoreGenre.#text' :  "Music"})

for item in cursor:
    print(item)

Search for records that include "Arkansas" in description

cursor = records.find({ 'pbcoreDescriptionDocument.pbcoreDescription.#text' :  {'$regex':'.*Arkansas.*'}})

for item in cursor:
    print(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment