Skip to content

Instantly share code, notes, and snippets.

@joshcass
Last active May 25, 2020 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshcass/2ea1ab9d26b7395d24187197442f9ff9 to your computer and use it in GitHub Desktop.
Save joshcass/2ea1ab9d26b7395d24187197442f9ff9 to your computer and use it in GitHub Desktop.
Query config.json description

There are 4 types of queries that are supported:

1

A query that gets an entire collection without any filtering. We have a few collections that represent internal data that doesn't need to be filtered by company. For these your json object would look like:

 {
    "name": "name_of_collection_you_want_to_get",
    "filter": "none",
  }

2

A query that uses a lookback for a number of days. Some of the collections that don't need a company filter are quite large and it's not necessary to wrap up gigs and gigs of historical data for development. So instead we can provide a lookback to reduce the number of documents that get pulled.

{
    "name": "name_of_collection_you_want_to_get",
    "filter": "lookback",
    "lookback": {
      "days": 7,
      "field": "created_at"
    }
 }

The lookback attributes are required when defining this type

3

A query that can be filtered by a company_id field. For these your json object would look like:

  {
    "name": "name_of_collection_you_want_to_get",
    "filter": "company"
  }

This will tell our go app to filter the collection on the Bonusly company_id

4

A query that uses the mongodb aggregate pipeline with a lookup This one is a little more complex. Many collections do not have a company_id but they do have an association with a record that does have a company_id For these your json object would look like:

{
    "name": "name_of_collection_you_want_to_get",
    "filter": "aggregate",
    "pipeline_attributes": {
      "starting_collection": "associated_collection_that_has_company_id",
      "stages": [
        {
          "name": "$lookup",
          "attributes": {
            "from": "name_of_collection_you_want_to_get",
            "localField": "_id",
            "foreignField": "id_stored_on_the_collection_you_want_that_matches_the_starting_collection",
            "as": "name_of_collection_you_want_to_get"
          }
        },
        {
          "name": "$unwind",
          "attributes": "$name_of_collection_you_want_to_get"
        },
        {
          "name": "$replaceRoot",
          "attributes": { "newRoot": "$name_of_collection_you_want_to_get" }
        }
      ]
    }
  }

The pipeline should start from a collection that has a company_id, this first filter is automatically included in the app code. The pipeline attibutes are requred for this type.

Removing a collection from the image is as simple as deleting the object that defines it's query in the JSON config file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment