Skip to main content

Filter Query

Functions which allow you to transform a specially crafted RESTful query into a MongoDB query. This middleware is extremely useful when building RESTful APIs with Elemental.

Raw Usage

import (
    "fmt"
    "github.com/elcengine/elemental/plugins/filterquery"
)

func main() {
  q := fq.Parse("?filter[name]=eq(Geralt)&filter[age]=gt(100)&sort=-name&select=name,age")
  fmt.Println(q.Filters) // map[string]interface{}{"name": map[string]interface{}{"$eq": "Geralt"}, "age": map[string]interface{}{"$gt": 100}}
}

Usage with Go Fiber

import (
    "github.com/gofiber/fiber/v2"
    "github.com/elcengine/elemental/plugins/filterquery/middleware"
)

func main() {
    app := fiber.New()

    app.Use(fqm.NewGoFiber())

    app.Get("/api/v1/witchers", func(ctx *fiber.Ctx) error {
        q := ctx.Locals(fqm.CtxKey).(fq.Result)
        witchers := WitcherModel.Find(q.Filters).Sort(q.Sorts).Select(q.Select).ExecTT()
        return ctx.JSON(witchers)
    })

    app.Listen(":3000")
}

Usage Shortcuts

  • QS

    • Construct an Elemental query directly from a request's query string. It uses the filterquery plugin to parse the query string and apply filters, sorting, lookups, and projections to the final query.

    witchers := WitcherModel.QS("filter[name]=Geralt").ExecTT()
    
  • QSR

    • Construct an Elemental query directly from a FilterQueryResult.


    witchers := WitcherModel.QSR(fq.Parse("filter[name]=Geralt")).ExecTT()
    

Example RESTful Queries and their MongoDB Equivalents

Simple Queries

  • Query: /api/v1/witchers?filter[name]=Geralt

    MongoDB Query:

    {
      "name": "Geralt"
    }
    
  • Query: /api/v1/witchers?filter[name]=eq(Geralt)

    MongoDB Query:

    {
      "name": {
        "$eq": "Geralt"
      }
    }
    
  • Query: /api/v1/witchers?filter[age]=gt(100)

    MongoDB Query:

    {
      "age": {
        "$gt": 100
      }
    }
    
  • Query: /api/v1/witchers?filter[age]=gte(100)

    MongoDB Query:

    {
      "age": {
        "$gte": 100
      }
    }
    
  • Query: /api/v1/witchers?filter[age]=lt(100)

    MongoDB Query:

    {
      "age": {
        "$lt": 100
      }
    }
    
  • Query: /api/v1/witchers?filter[age]=lte(100)

    MongoDB Query:

    {
      "age": {
        "$lte": 100
      }
    }
    
  • Query: /api/v1/witchers?filter[retired]=true

    MongoDB Query:

    {
      "retired": true
    }
    
  • Query: /api/v1/witchers?filter[retired]=false

    MongoDB Query:

    {
      "retired": false
    }
    
  • Query: /api/v1/witchers?filter[weapons]=in(Silver Sword,Steel Sword)

    MongoDB Query:

    {
      "weapons": {
        "$in": ["Silver Sword", "Steel Sword"]
      }
    }
    
  • Query: /api/v1/witchers?filter[weapons]=nin(Silver Sword,Steel Sword)

    MongoDB Query:

    {
      "weapons": {
        "$nin": ["Silver Sword", "Steel Sword"]
      }
    }
    
  • Query: /api/v1/witchers?filter[name]=reg(Geralt)

    MongoDB Query:

    {
        "name": {
            "$regex": /Geralt/
        }
    }
    
  • Query: /api/v1/witchers?filter[school]=exists(true)

    MongoDB Query:

    {
      "school": {
        "$exists": true
      }
    }