/opt/restheart/etc/restheart.yml
(before was /rh/etc/restheart.yml
)
@RegisterPlugin(name = "csvTransformer",
interceptPoint = InterceptPoint.RESPONSE,
description = "transform the response to CSV format",
enabledByDefault = true)
public class CsvTransformer implements MongoInterceptor {
@Override
public void handle(MongoRequest request, MongoResponse response) {
var docs = response.getContent().asArray();
var sb = new StringBuilder();
// add the header
if (docs.size() > 0) {
docs.get(0).asDocument().keySet().forEach(k -> sb.append(k).append(","));
sb.append("\n");
}
// add rows
docs.stream()
.map(BsonValue::asDocument)
.forEach(fdoc -> {
sb.append(fdoc.entrySet().stream()
.map(e -> e.getValue())
.map(v -> BsonUtils.toJson(v))
.collect(Collectors.joining(",")));
sb.append("\n");
});
response.setContentType("text/csv");
response.setCustomSender(() -> response.getExchange().getResponseSender().send(sb.toString()));
}
@Override
public boolean resolve(MongoRequest request, MongoResponse response) {
return request.isGet()
&& request.isCollection()
&& response.getContent() != null
&& request.getQueryParameterOfDefault("csv", null) != null;
}
}
$ http -b -a admin:secret :8080/coll\?csv
_id,a,_etag,
{"$oid":"6202562ce5078606d08b79e2"},1,{"$oid":"6202562ce5078606d08b79e1"}
{"$oid":"62025626e5078606d08b79df"},1,{"$oid":"62025662e5078606d08b79e5"}
GET /coll?page=1
, GET /coll?page=2
,…. until you reach the page with an empty array.
## Read Performance
# default-pagesize is the number of documents returned when the pagesize query
# parameter is not specified
# see https://restheart.org/docs/mongodb-rest/read-docs#paging
default-pagesize: 100
# max-pagesize sets the maximum allowed value of the pagesize query parameter
# generally, the greater the pagesize, the more json serializan overhead occurs
# the rule of thumb is not exeeding 1000
max-pagesize: 1000
# cursor-batch-size sets the mongodb cursor batchSize
# see https://docs.mongodb.com/manual/reference/method/cursor.batchSize/
# cursor-batch-size should be smaller or equal to the max-pagesize
# the rule of thumb is setting cursor-batch-size equal to max-pagesize
# a small cursor-batch-size (e.g. 101, the default mongodb batchSize)
# speeds up requests with small pagesize
cursor-batch-size: 1000
ERROR o.r.mongodb.handlers.ErrorHandler - Error handling the request
com.mongodb.MongoQueryException: Query failed with error code 292 and error message 'Executor error during find command :: caused by :: Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.' on server
allowDiskUse
?avars={"var1":1, "var2": {"an": "object}}
?page
and /pagesize
values as parameters, see https://restheart.org/docs/mongodb-rest/aggregations/#predefined-variables
filter
is there a way to limit (or make unlimited) the number of returned documents? I noticed that it returns 100 only. How do I get all? Or limit to 10 only? I know RESTHeart supports pagination, but I can't figure out how to use it. Thank you!!
?page=x
to ask for page number x