Skip to content

Using MongoDB filters with the Slothpixel API

builder_247 edited this page Mar 13, 2021 · 2 revisions

⚠️ The information described in this page is no longer relevant, as MongoDB is no longer used by Slothpixel


Using MongoDB filters with the Slothpixel API

In the following example we use advanced queries for auctions. The same filter syntax can however be used for the dynamic leaderboard endpoint as well.

Let's say we want to get all historic auctions for items that have

  • Sharpness enchantment level 3 or above
  • Starting bid of less or equal than 100
  • Origin from the dark auction or bought from an NPC

In order to achieve these special conditions we use the filter parameter.

MongoDB queries

MongoDB query operators allow filtering databases based on set conditions. See the link for a full list of operators and their example usage.

Our query in JSON looks like this:

{
  "starting_bid": {
    "$lte": 500  
  },
  "item.attributes.enchantments.sharpness": {
    "$gt": 3
  },
  "$or": [
    {
      "item.attributes.origin": {
        "$eq": "DARK_AUCTION"
      }
    },
    {
      "item.attributes.origin": {
        "$eq": "SHOP_PURCHASE"
      }
    }
  ]
}

Encoding

In order to send the query string to slothpixel servers, it first has to be URL encoded.

Now the next step isn't necessary but it makes the query look prettier and also shortens the URL. We minify the JSON string, i.e. remove the spaces using javascript's JSON.stringify(jsonObject, null, 0) function producing the following string:

{"starting_bid":{"$lte":500},"item.attributes.enchantments.sharpness":{"$gt":3},"$or":[{"item.attributes.origin":{"$eq":"DARK_AUCTION"}},{"item.attributes.origin":{"$eq":"SHOP_PURCHASE"}}]}

In javascript we can use the encodeURI() function for the URL encoding. After passing our query JSON string to the encode function it looks like this:

%7B%22starting_bid%22:%7B%22$lte%22:500%7D,%22item.attributes.enchantments.sharpness%22:%7B%22$gt%22:3%7D,%22$or%22:%5B%7B%22item.attributes.origin%22:%7B%22$eq%22:%22DARK_AUCTION%22%7D%7D,%7B%22item.attributes.origin%22:%7B%22$eq%22:%22SHOP_PURCHASE%22%7D%7D%5D%7D

The request

Now we need to pass the decoded string as the filter parameter in the request. Using the active=false parameter allows us to also include historical auctions stored on the Slothpixel database.

Final request:

GET https://api.slothpixel.me/api/skyblock/auctions?active=false&filter=%7B%22starting_bid%22:%7B%22$lte%22:500%7D,%22item.attributes.enchantments.sharpness%22:%7B%22$gt%22:3%7D,%22$or%22:%5B%7B%22item.attributes.origin%22:%7B%22$eq%22:%22DARK_AUCTION%22%7D%7D,%7B%22item.attributes.origin%22:%7B%22$eq%22:%22SHOP_PURCHASE%22%7D%7D%5D%7D

Now, if you did everything correctly the server should return the filtered response.