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.]

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
[]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s