MongoDB and Python: A Simple Example

The code below demonstrates how to use Python to connect to a MongoDB database. I chose to use a cloud based instance of MongoDB provided free of charge by MongoLab.com. The script demonstrates how to:

  • Use the PyMongo library to connect to a Mongo database
  • Insert documents into a collection
  • Display all of the documents from the collection

I also used a local instance of MongoDB for testing. You will will need to use a Python package manager such as EasyInstall to install the PyMongo library.

Here is the Python code:

# mongo_hello_world.py
# Author: Bruce Elgort
# Date: March 18, 2014
# Purpose: To demonstrate how to use Python to
# 1) Connect to a MongoDB document collection
# 2) Insert a document
# 3) Display all of the documents in a collection</code>

from pymongo import MongoClient

# connect to the MongoDB on MongoLab
# to learn more about MongoLab visit http://www.mongolab.com
# replace the "" in the line below with your MongoLab connection string
# you can also use a local MongoDB instance
connection = MongoClient("yourmongodbconnectionstring")

# connect to the students database and the ctec121 collection
db = connection.students.ctec121

# create a dictionary to hold student documents

# create dictionary
student_record = {}

# set flag variable
flag = True

# loop for data input
while (flag):
   # ask for input
   student_name,student_grade = input("Enter student name and grade: ").split(',')
   # place values in dictionary
   student_record = {'name':student_name,'grade':student_grade}
   # insert the record
   db.insert(student_record)
   # should we continue?
   flag = input('Enter another record? ')
   if (flag[0].upper() == 'N'):
      flag = False

# find all documents
results = db.find()

print()
print('+-+-+-+-+-+-+-+-+-+-+-+-+-+-')

# display documents from collection
for record in results:
# print out the document
print(record['name'] + ',',record['grade'])

print()

# close the connection to MongoDB
connection.close()

If you have any questions please let me know.

Want to Learn More About IBM’s Mail.Next?

Date: Apr 23, 2014
Time: 12:00 PM – 1:00 PM (Eastern Time)
Hosted by: SocialBiz UG.org
Presented by: SocialBiz UG.org, Kramer Reeves (Director – Messaging & Collaboration Solutions, IBM Software Group), Scott Souder (Program Director and Sr. Product Manager responsible for IBM Mail Next)

One of the hottest topics at IBM Connect 2014 was the introduction of IBM Mail Next, announced during the opening general session. IBM Mail Next will help you Focus on, Find and Take Control of your mail-based actions and priorities. Register today to join Kramer Reeves, Director – Messaging & Collaboration Solutions, IBM Software Group, Scott Souder, Program Director and Sr. Product Manager responsible for IBM Mail Next, along with members of the Mail Next team to learn about the design advancements since January and see the plan for releasing IBM Mail Next to market.

Attend this webcast to learn:

  • How IBM Mail Next is designed to provide productivity boosts for your organization
  • About the latest design concepts and user experience
  • What the future holds for packaging and delivery of IBM Mail Next
  • How you can begin your journey to IBM Mail Next today with iNotes + Notes Browser Plug-in

Join this webcast to learn how you and your organization will be able to reap the benefits of IBM Mail Next!

Register >

Registration is Now Open for MongoDB University

Take free online courses from the creators of MongoDB. MongoDB University offers courses for both developers and DBAs.

  • Seven week courses give you flexibility to work through material when it suits your schedule.
  • Frequent assessments and check-ins by TAs and instructors help you verify your understanding.
  • A certificate of completion confirms your membership in our community of cutting-edge NoSQL technologists.

More than 100,000 people have enrolled in our courses to date.

Register now >

Technology and Blind Students

Here’s a video my friend Andrew Pennington, a student at the Washington State School for the Blind made:

Here is a some more information about my recent experience with Andrew while teaching at Clark College that was originally shared on Facebook:

What a wonderful day it has been. My job shadow student from the Washington State School for the Blind spent the day working with me on class prep, code examples, a tour of the campus and then spent 2 hours in my PHP class. The students in the PHP class made him feel part of the class. He also shared with the class his plans for becoming a programmer and his plans to attend WSU. He was also very witty, which for those of you who know my PHP students, fit in perfectly

Another thing that truly struck a chord with me was that he has the same visual acuity as I do. Throughout the day I kept flashing back to myself at his age.

Again, what an awesome experience it was to host him at Clark College — Vancouver, Washington today.

You can also follow Andrew on Twitter.

OneNote Now on Mac and Free

When we started OneNote we set out to revolutionize the way people capture, annotate, and recall all the ideas, thoughts, snippets and plans in their life. As many of you have attested, OneNote is the ultimate extension for your brain, but it’s not complete if it’s not instantly available everywhere. We’ve already made a lot of progress in that direction with our mobile, tablet and online web experiences. But there was still a gap. People frequently asked us for OneNote on Mac, and for more ways to capture content.

onenotemacMore >

How to Learn AngularJS

The ng-newsletter site has a nice comprehensive article on how to get started and learn AngularJS:

Learning AngularJS can be complex. There are an overwhelming number resources available on the web. The blog posts can be conflicting and confusing, and a simple google search can turn up 5 articles all describing the same thing in a completely different way.

This guide is written to be your roadmap; your personal guide of how to learn AngularJS. We’ll we’ll through the basic concepts of AngularJS in a logical, conceptual order. This post is intended to be the first post to read when starting to learn AngularJS. Use this post as your guide and you’ll be mastering Angular in no time. Let’s get started!

More >