
import json

import bottle
from bottle_mongo import MongoPlugin


app = bottle.Bottle()

app.config.load_config('settings.ini')

app.install(MongoPlugin(
    uri=app.config['mongodb.uri'],
    db=app.config['mongodb.db'],
    json_mongo=True))


def _get_json(name):
    result = bottle.request.query.get(name)
    return json.loads(result) if result else None


@app.route('/<collection>')
def get(mongodb, collection):
    filter_ = _get_json('filter')
    projection = _get_json('projection')
    skip = int(bottle.request.query.get('skip', 0))
    limit = int(bottle.request.query.get('limit', 100))
    sort = _get_json('sort')

    cursor = mongodb[collection].find(
        filter=filter_, projection=projection, skip=skip, limit=limit,
        sort=sort
    )

    return {'results': [document for document in cursor]}


if __name__ == '__main__':
    app.run()


import json

import bottle
from bottle_mongo import MongoPlugin


app = bottle.Bottle()

app.config.load_config('settings.ini')

app.install(MongoPlugin(
    uri=app.config['mongodb.uri'],
    db=app.config['mongodb.db'],
    json_mongo=True))


def _get_json(name):
    result = bottle.request.query.get(name)
    return json.loads(result) if result else None


@app.route('/<collection>')
def get(mongodb, collection):
    filter_ = _get_json('filter')
    projection = _get_json('projection')
    skip = int(bottle.request.query.get('skip', 0))
    limit = int(bottle.request.query.get('limit', 100))
    sort = _get_json('sort')
    # Turns a JSON array of arrays to a list of tuples.
    sort = [tuple(field) for field in sort] if sort else None

    cursor = mongodb[collection].find(
        filter=filter_, projection=projection, skip=skip, limit=limit,
        sort=sort
    )

    return {'results': [document for document in cursor]}


if __name__ == '__main__':
    app.run()

Sort needs to be an array of arrays in the query string but a list of tuples in python.
Bugfix: Sort needs to be an array of arrays in the query string but a list of tuples in python.

  
  import json
  
  import bottle
  from bottle_mongo import MongoPlugin
  
  
  app = bottle.Bottle()
  
  app.config.load_config('settings.ini')
  
  app.install(MongoPlugin(
      uri=app.config['mongodb.uri'],
      db=app.config['mongodb.db'],
      json_mongo=True))
  
  
  def _get_json(name):
      result = bottle.request.query.get(name)
      return json.loads(result) if result else None
  
  
  @app.route('/<collection>')
  def get(mongodb, collection):
      filter_ = _get_json('filter')
      projection = _get_json('projection')
      skip = int(bottle.request.query.get('skip', 0))
      limit = int(bottle.request.query.get('limit', 100))
      sort = _get_json('sort')
+     # Turns a JSON array of arrays to a list of tuples.
+     sort = [tuple(field) for field in sort] if sort else None
  
      cursor = mongodb[collection].find(
          filter=filter_, projection=projection, skip=skip, limit=limit,
          sort=sort
      )
  
      return {'results': [document for document in cursor]}
  
  
  if __name__ == '__main__':
      app.run()
  
Sort needs to be an array of arrays in the query string but a list of tuples in python.
## Code Before:

import json

import bottle
from bottle_mongo import MongoPlugin


app = bottle.Bottle()

app.config.load_config('settings.ini')

app.install(MongoPlugin(
    uri=app.config['mongodb.uri'],
    db=app.config['mongodb.db'],
    json_mongo=True))


def _get_json(name):
    result = bottle.request.query.get(name)
    return json.loads(result) if result else None


@app.route('/<collection>')
def get(mongodb, collection):
    filter_ = _get_json('filter')
    projection = _get_json('projection')
    skip = int(bottle.request.query.get('skip', 0))
    limit = int(bottle.request.query.get('limit', 100))
    sort = _get_json('sort')

    cursor = mongodb[collection].find(
        filter=filter_, projection=projection, skip=skip, limit=limit,
        sort=sort
    )

    return {'results': [document for document in cursor]}


if __name__ == '__main__':
    app.run()

## Instruction:
Sort needs to be an array of arrays in the query string but a list of tuples in python.
## Code After:

import json

import bottle
from bottle_mongo import MongoPlugin


app = bottle.Bottle()

app.config.load_config('settings.ini')

app.install(MongoPlugin(
    uri=app.config['mongodb.uri'],
    db=app.config['mongodb.db'],
    json_mongo=True))


def _get_json(name):
    result = bottle.request.query.get(name)
    return json.loads(result) if result else None


@app.route('/<collection>')
def get(mongodb, collection):
    filter_ = _get_json('filter')
    projection = _get_json('projection')
    skip = int(bottle.request.query.get('skip', 0))
    limit = int(bottle.request.query.get('limit', 100))
    sort = _get_json('sort')
    # Turns a JSON array of arrays to a list of tuples.
    sort = [tuple(field) for field in sort] if sort else None

    cursor = mongodb[collection].find(
        filter=filter_, projection=projection, skip=skip, limit=limit,
        sort=sort
    )

    return {'results': [document for document in cursor]}


if __name__ == '__main__':
    app.run()
