Python


Python & IronPython & MongoDB27 May 2010 04:22 pm

While most .NET developers who want to work with MongoDB focus on C# with NoRM and MongoDB-CSharp, there’s an alternative - IronPython and PyMongo.

PyMongo is the 10Gen developed Python driver for MongoDB. It’s actually pretty easy to get it working with IronPython. The steps are as follows:

  1. Download PyMongo via easy_install pymongo
  2. Copy the PyMongo egg from Python’s site-packages directory to IronPython’s site packages directory.
  3. Open the egg from with any program that opens zip files. An easy option, just rename the extension to zip and double click with Windows Explorer…
  4. Copy the pymongo and gridfs directories out of the zip and paste them right into site-packages.

That’s pretty much it. Python eggs are apparently incompatible with IronPython, so you can’t just drop the egg in site-packages. There may a way around that of which I’m unaware.

Finally open up ipy.exe and try:

IronPython 2.6 (2.6.10920.0) on .NET 2.0.50727.4927
Type "help", "copyright", "credits" or "license" for more information.
>>> from pymongo import *
>>> conn = Connection("localhost", 27017)
>>> db = conn.blog
>>> posts = db.posts
>>> posts.insert({"Title" : "On Using PyMongo with IronPython", "Author" : "John Zablocki"})
ObjectId('4bfee0c4af6bd912f4000000')
>>> posts.insert({"Title" : "On Installing PyMongo as a Service on Windows", "Author" : "John Zablocki"})
ObjectId('4bfee0e3af6bd912f4000001')
>>> posts.find({ "Author" : "John Zablocki"})

>>> posts.find({ “Author” : “John Zablocki”})[0]
{’Author’: ‘John Zablocki’, ‘_id’: ObjectId(’4bfee0c4af6bd912f4000000′), ‘Title’
: ‘On Using PyMongo with IronPython’}
>>> posts.update({ “Author” : “John Zablocki”}, { “Author” : “John C. Zablocki”})
>>> posts.find()[0]
{’Author’: ‘John C. Zablocki’, ‘_id’: ObjectId(’4bfee0c4af6bd912f4000000′)}

I haven’t played with it too much yet, but the basic CRUD functionality all seems pretty solid.

Python & Google21 Apr 2009 07:11 pm

C# & Python & IronPython & JavaScript06 Dec 2008 12:33 pm

C# & Python & Ruby20 Mar 2008 08:42 pm

Teaching & Python & Eclipse & Mac27 Jan 2008 02:53 pm