Search code examples
pythonmongodbmongoenginerestframeworkmongoengine

Unable to access mongo entry by "id"


I have a Mongo Document with some fields (_id, id, name, status, etc...). I wrote a typical document in a class(like a model would do):

class mod(Document):
    id=fields.IntField()  
    name = fields.StringField()
    status=fields.StringField()
    description_summary = fields.StringField()
    _id = fields.ObjectIdField()

And with this model, I tried to access them:

    >>> from mongoengine import *
    >>> from api.models import *
    >>> connect('doc')
    MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True, read_preference=Primary())

I tried to fetch all the entries in the "mod" document: It Worked! I can get all the fields of all the entries (id, name, etc...)

    >>> mod_ = mod.objects.all()
    >>> mod_[0].name
    'Name of entry'
    >>> mod_[0].id
    102

I tried to filter and return all the entries with the field "status" = "Incomplete": It works, just like before.I tried to filter other fields: it works too

    >>> mod_ = mod.objects(status="Incomplete")
    >>> mod_[0].name
    'Name of entry'
    >>> mod_[0].id
    102

But When I try to filter with the id I don't manage to get a result:

    >>> mod_ = mod.objects(id=102)
    >>> mod_[0].name
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "/.../lib/python3.4/site-packages/mongoengine/queryset/base.py", line 193, in __getitem__
        return queryset._document._from_son(queryset._cursor[key],
      File "/.../lib/python3.4/site-packages/pymongo/cursor.py", line 570, in __getitem__
        raise IndexError("no such item for Cursor instance")
    IndexError: no such item for Cursor instance
    >>> mod_[0].id
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "/.../lib/python3.4/site-packages/mongoengine/queryset/base.py", line 193, in __getitem__
        return queryset._document._from_son(queryset._cursor[key],
      File "/.../lib/python3.4/site-packages/pymongo/cursor.py", line 570, in __getitem__
        raise IndexError("no such item for Cursor instance")
    IndexError: no such item for Cursor instance

So I tried with mod.objects.get(id=102)

>>> mod_ = mod.objects.get(id=102)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/.../lib/python3.4/site-packages/mongoengine/queryset/base.py", line 271, in get
    raise queryset._document.DoesNotExist(msg)
api.models.DoesNotExist: mod matching query does not exist.

Mod matching query does not exist, so it doesn't recognize the id field but when I write mod_[0].id I do have a result, so what can be wrong?

EDIT: I believe that when writing mod.objects(id=102), the field id is interpreted as the _id. How can I specify, I want to query by id and not _id? My Document is already written, so I cannot change the name of the fields.


Solution

  • So, the problem does not come from the difference between _id and id, like said @HourGlass. The values of the id field were stored as integer, I wrote fields.IntField() for the id field, and called mod.objects(id=102) (without quotes).

    But for some reason, I have to write them as fields.StringField(), and call mod.objects(id='102').