111

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\test.py", line 2, in <module>
    import pymongo
  File "C:\Python27\lib\site-packages\pymongo\__init__.py", line 80, in <module>
    from pymongo.connection import Connection
  File "C:\Python27\lib\site-packages\pymongo\connection.py", line 39, in <module>
    from pymongo.mongo_client import MongoClient
  File "C:\Python27\lib\site-packages\pymongo\mongo_client.py", line 45, in <module>
    from pymongo import (auth,
  File "C:\Python27\lib\site-packages\pymongo\database.py", line 22, in <module>
    from pymongo.collection import Collection
  File "C:\Python27\lib\site-packages\pymongo\collection.py", line 25, in <module>
    from pymongo.cursor import Cursor
  File "C:\Python27\lib\site-packages\pymongo\cursor.py", line 19, in <module>
    from bson import RE_TYPE
ImportError: cannot import name RE_TYPE
vabada
  • 1,738
  • 4
  • 29
  • 37
Gerswin Lee
  • 1,265
  • 2
  • 11
  • 12

1 Answers1

237

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)})]
Evgenii
  • 3,283
  • 3
  • 27
  • 32
  • I update pymongo to 2.5 - all working. Can you see error message and your code? – Evgenii Apr 19 '13 at 08:14
  • @GerswinLee notice the "from bson.objectid"? You'll get an import error trying to import it from pymongo in version 2.5 You need python-bson package. I'm not sure if that's installed with pymongo or not. – hyprnick Apr 28 '13 at 05:55
  • For me to get it to work, I had to turn `obj_id_to_find` into a string i.e. `str(obj_id_to_find)` – luke Jan 01 '17 at 11:01