On Wed, 12 Apr 2017 09:30:08 GMT, Petter Viklund wrote:

I want to fetch data from several collections and append that to a json output. Have done it in Python(flask and pymongo) but want to use Vibe.d.
Is it possible to do someting like this in vibe.d or rather get the same end result.

Example on how it looks right now in Python:

output = []
common = mongo.db.common //
for q in common.find():
    output.append({'aktcel': q['aktcel'], 'nxtcel': q['nxtcel'],'aktopr': q['aktopr'],'nxtcpn': q['nxtcpn']} )

celltab = mongo.db.celltab
for qc in celltab.find({'number': q['aktcel']}):
    output.append({'aktname': qc['name'],'t1': qc['t1'],'t2':qc['t2'],'t3':qc['t3'],'t4':qc['t4']})

for qnc in celltab.find({'number': q['nxtcel']}):
    output.append({'nxtname': qnc['name']})

return jsonify(output)

I can explain a little more about it if it's not clear what I mean.

Is the indentation correct in the example above? More specifically, shouldn't the two lower loops be part of the first one, so that they can access the value of each q there?

The most efficient route would be to create types that represent the different database documents. In this case, there are some complications due to the name change of the cells, which can be solved either using a serialization policy or by manually assembling the JSON document (like in the Python example):

import vibe.vibe, std.algorithm, std.array;

struct JsonPol {} // used to customize the JSON result

struct CommonDoc {
    // could also use a specific type, such as int or string
    Json aktcel, nxtcel, aktopr, nxtcpn;

struct CellTabDoc {
    // uses "aktname" instead of "name" if JsonPol is used as the
    // serialization policy
    @name!JsonPol("aktname") Json name;
    Json t1, t2, t3, t4;

string generateResult()
    Json[] output;
    foreach (q; common.find!CommonDoc()) {
        output ~= q.serializeToJson();
        output ~= celltab.find!CellTabDoc(["number": q.aktcel])
            .map!(qc => qc.serializeWithPolicy!(JsonSerializer, JsonPol))
        output ~= celltab.find(["number": q.nxtcel])
            .map!(qnc => Json(["nxtname": qnc["name"]])
    return output.serializeToJsonString();

Note: written off the top of my head, so might contain some mistakes.