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:
- Download PyMongo via easy_install pymongo
- Copy the PyMongo egg from Python’s site-packages directory to IronPython’s site packages directory.
- 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…
- 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.
Pingback: DotNetShoutout
Thanks much for this. I am going to try this oo-db.
I have a question. What about indexes in OO DBs? Is there a need for indexing cols and if how it can be done?
thx
Bernd
Hello,
i am trying to use the pymongo-driver in an asp.net-project using ironpython. I was able to install it successfully for the ironpython command line version. There is a quit good tutorial on the weblog from John Zablocki
Works good as far as I have seen.
But with my asp.net application using ipy I was not so lucky. I got an error:
Parser Error Message: No module named collections
Here is my test code-behind:
webform.aspx.py——————————————————————————————–
from pymongo import Connection
def Page_Load(sender, e):
connection = Connection()
def Button1_Click(sender, e):
pass
—————————————————————————————————————
In the Global.py I have made following configuration:
def Application_Start():
‘ Code that runs on application startup’
# if you want to use stdrt-py-libs in yout iron-python app add the folder
import sys
sys.path.append(‘C:\\Python25\\Lib’)
sys.path.append(‘C:\\Python25\\Lib\\site-packages’)
Anyone here who can tell me what’s wrong?
And BTW does someone here tried the mongo-db with ipy and the clr-driver?
Thanks much for your help & kind regards
Bernd