2

I'm getting this error when I try to connect to mongodb (using pymongo) with Iron Python...

Traceback (most recent call last):
  File "test.py", line 3, in <module>
  File "c:\Program Files (x86)\IronPython 2.7\lib\site-packages\pymongo\connecti
on.py", line 179, in __init__
  File "c:\Program Files (x86)\IronPython 2.7\lib\site-packages\pymongo\mongo_cl
ient.py", line 269, in __init__
pymongo.errors.ConnectionFailure: Specified cast is not valid.

Code is pretty simple, I have replaced the db name.

import pymongo

c = pymongo.Connection('mongodb://testuser:test123@linus.mongohq.com:10021/sometestdb')

It works fine with regular python. Any ideas?

laxonline
  • 2,657
  • 1
  • 20
  • 37
user1502301
  • 565
  • 1
  • 4
  • 16
  • 1
    Best way to debug: What are those lines of source code that are failing (it's open source)? The unfortunate answer is likely that something is missing from IronPython that pymongo requires. – WiredPrairie Jan 30 '13 at 15:07
  • PyMongo tends to obscure the underlying error in cases like this, so following the line number won't help as much as you want. You may want to temporarily remove all of the try/except clauses in mongo_client.py to help debug. But the basic answer is: PyMongo doesn't support IronPython. – A. Jesse Jiryu Davis Jan 30 '13 at 16:09
  • You might look at this: http://dllhell.net/2010/05/27/on-using-pymongo-with-ironpython/ – mjhm Jan 30 '13 at 21:07

2 Answers2

3

Ironpython isn't supported by pymongo - so I wouldn't advise trying to use it. You can see on the pypi page a list of supported implementations: http://pypi.python.org/pypi/pymongo

Ross
  • 17,861
  • 2
  • 55
  • 73
0

Please also see the answer here: Working with PTVS, IronPython and MongoDB


You may not be able to use pymongo with IronPython, but you can use the C#/.NET driver for MongoDB from IronPython.

Information on the driver is here. As explained in this link, you can install with nuget (PM> Install-Package mongocsharpdriver), or just download the dlls.

Once installed, you can use the assemblies in the normal way in IronPython:

    # Add reference to the Mongo C# driver
    import clr
    clr.AddReferenceToFileAndPath("MongoDB.Bson.dll")
    clr.AddReferenceToFileAndPath("MongoDB.Driver.dll")

Then use according to the MongoDB C# Driver API, for example:

    # Get the MongoDB database
    from MongoDB.Driver import MongoClient
    client = MongoClient("mongodb://localhost")
    server = client.GetServer()
    database = server.GetDatabase("test")

    # Get a collection
    collection = database.GetCollection("users")

    # Add a document
    from MongoDB.Bson import BsonDocument
    user = BsonDocument({'first_name':'John', 'last_name':'Smith'})
    collection.Insert(user)

See the the MongoDB C# Driver API for more information.

Community
  • 1
  • 1
Captain Whippet
  • 2,143
  • 4
  • 25
  • 34