Search code examples
pythonmongodbresteve

Using integer as type of _id return HTTP 400 for item_lookup


My setting is something like below, which the main identifier _id use integer type.

When I tried to access an item (it's exist on the db) using this URL: http://xxxxxxx/promotions/1485919019667 , it returns HTTP 404.

I wonder whether it's not possible to use type other than ObjectID for _id ?

promotions = {
    'item_title': 'promotion',
    'item_url' : 'regex("[\d]+")',
    'item_lookup': True,
    'schema': {
        '_id': {
            'type': 'integer',
            'minlength': 13,
            'maxlength': 13,
            'required': True,
            'unique': True
        },
        'category': {
            'type': 'string',
            'allowed': ["MARKETPLACE", "ELAUGETS", "FASHNESS", "FRINKS", "TRENTER", "OTHERS"]
        },
        'card_id': {
            'type': 'string',
            'minlength': 1,
            'maxlength': 50
        },
        'title': {
            'type': 'string',
            'minlength': 5,
            'maxlength': 200,
            'required': True
        },
        'description': {
            'type': 'string',
            'minlength': 5,
            'maxlength': 2500,
            'required': True
        }
    }
}

Solution

  • Have you looked at Handling custom ID fields in the documentation? Quoting from the heading of that page:

    However, you might have collections where your unique identifier is not and ObjectId, and you still want individual document endpoints to work properly. Don’t worry, it’s doable, it only requires a little tinkering.

    The tutorial covers UUID fields, but is good for any custom type really.