MongoDB CRUD with Pymongo

Now here’s code for performing CRUD operations on MongoDB using pymongo module of Python.

In this example, we

  • Create a collection
  • Then Read it to check if create was successful
  • Update the collection and
  • then Delete the collection

It’s important to note that MongoDB uses BSON formation for data storage and network transfers.. [You need to take care of converting them to appropriate format for use.]

https://gist.github.com/3167821.js

Output of this program looks like this

After create
[{u'grade': u'Boring', u'_id': ObjectId('504dad241d41c81ae98874ce'), u'id': u'1', u'name': u'C'}, {u'grade': u'Interesting', u'_id': ObjectId('504dad241d41c81ae98874cf'), u'id': u'2', u'name': u'Python'}]
After update
[{u'grade': u'Interesting', u'_id': ObjectId('504dad241d41c81ae98874cf'), u'id': u'2', u'name': u'Python'}, {u'grade': u'Make it interesting', u'_id': ObjectId('504dad241d41c81ae98874ce'), u'id': u'1', u'name': u'C'}]
After delete
[]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.