Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

Eve - Is it possible to unset a key from a document?

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...