3

I defined a resource with this schema

# 'people' schema definition
'schema'= {
    'firstname': {
        'type': 'string',
        'minlength': 1,
        'maxlength': 10,
    },
    'lastname': {
        'type': 'string',
        'minlength': 1,
        'maxlength': 15,
        'required': True,
        'unique': True,
    },
    # 'role' is a list, and can only contain values from 'allowed'.
    'role': {
        'type': 'list',
        'allowed': ["author", "contributor", "copy"],
    },
    # An embedded list of 'strongly-typed' dictionary.
    'locations': {
        'type': 'list',
        'schema' {
            'type': 'dict',
            'schema': {
                'address': {'type': 'string'},
                'city': {'type': 'string'}
            },
        },
    },
    'born': {
        'type': 'datetime',
    },
}

Is it possible to define the location as sub resource by referencing the same data source of people and perform search on it?

e.g. people/< firstname>/locations/< city>

Reply: list of locations

The reason why I need to have locations stored inside the people document is for taking advantage of the document atomicity in mongo.

I might need to update several locations of a person in one transaction. Therefor I want to make sure that all needed locations of a person ( or none) are updated. No intermediate state possible.

1 Answers1

2

Unfortunately that is not possible (dotted notation in regexes, amongst other things). However, nothing prevents you (or you client app) from querying the people endpoint like this:

people?where={"firstname": "<firstname>", "locations.city": "<city>"}

This is also ideal performance-wise. Writes are still atomic, queries are optmized once you build your indexes properly.

Nicola Iarocci
  • 6,606
  • 1
  • 20
  • 33