Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bliotti/c4f3e16c277e3aad70a40a8d0ee334ba to your computer and use it in GitHub Desktop.
Save bliotti/c4f3e16c277e3aad70a40a8d0ee334ba 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)
@vogeaux
Copy link

vogeaux commented Aug 7, 2022

"records.find" why import ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment