Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In a schema with optional values such as code in the example:

'code': {
    'type': 'string',
},
'name': {
    'type': 'string',
    'required': True,
},
'email': {
    'type': 'string',
    'required': True
}

Let's say there's an inserted document with a value for code. Can I unset the code key like mongodb $unset does, using Eve somehow?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
242 views
Welcome To Ask or Share your Answers For Others

1 Answer

One way to achieve this is to setup a default projection for the endpoint.

Limiting the Fieldset Exposed by the API Endpoint By default API responses to GET requests will include all fields defined by the corresponding resource schema. The projection setting of the datasource resource keyword allows you to redefine the fields.

people = {
    'datasource': {
        'projection': {'username': 1}
    }
}

The above setting will expose only the username field to GET requests, no matter the schema defined for the resource.

Another option is to leverage MongoDB Aggregation Framework itself. Just set the endpoint so that a aggregation is performed before data is returned to the client. The following should work (see the docs for details):

posts = {
    'datasource': {
        'aggregation': {
            'pipeline': [{"$unset": "code"}]
        }
    }
}

You need Eve v0.7 for aggregation support.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...