search by ObjectId in mongodb with pymongo


I need to search an ObjectId with python using pymongo but I always get this error. Any ideas how to search?

import pymongo
from pymongo import MongoClient
from pymongo import ObjectId

gate = collection.find({'_id': ObjectId(modem["dis_imei"])})
print gate

    Traceback (most recent call last):
  File "C:\Users\gerswin\Documents\Proyectos\Demonio py\", line 2, in <module>
    import pymongo
  File "C:\Python27\lib\site-packages\pymongo\", line 80, in <module>
    from pymongo.connection import Connection
  File "C:\Python27\lib\site-packages\pymongo\", line 39, in <module>
    from pymongo.mongo_client import MongoClient
  File "C:\Python27\lib\site-packages\pymongo\", line 45, in <module>
    from pymongo import (auth,
  File "C:\Python27\lib\site-packages\pymongo\", line 22, in <module>
    from pymongo.collection import Collection
  File "C:\Python27\lib\site-packages\pymongo\", line 25, in <module>
    from pymongo.cursor import Cursor
  File "C:\Python27\lib\site-packages\pymongo\", line 19, in <module>
    from bson import RE_TYPE
ImportError: cannot import name RE_TYPE
4/13/2016 11:38:25 AM

Accepted Answer

I use pymongo 2.4.1.

from bson.objectid import ObjectId
[i for i in dbm.neo_nodes.find({"_id": ObjectId(obj_id_to_find)})]
4/18/2013 5:09:13 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow