Skip to content

Searching The API

Tyler Lartonoix edited this page Mar 11, 2018 · 14 revisions

Any endpoint can be searched by passing a q parameter in the URL, i.e. /skills?q=.... The value of q should be a MongoDB-style query document. For those that are not familiar with Mongo query documents, a short explanation can be found below.

Query Document

A "query document" is simply a JSON object that defines fields and values to search by. For example, a request to https://mhw-db.com/skills?q={"name":"Poison%20Resistance"} would return a collection containing all skills whose name matched the string "Poison Resistance" exactly.

You can enhance your searches by using operators in your document, a list of which can be found here. For example, a request to https://mhw-db.com/armor?q={"attributes.defense":{"$gt":50}} would return any armor with a defense that is greater than 50.

Related Objects

You can also search fields that are related or nested in an object. A request to https://mhw-db.com/skills?q={"ranks.level":3} would return a collection containing all skills that have a rank of 3. A request to https://mhw-db.com/armor?q={"attributes.defense":40} would return all armor that has exactly 40 defense.

Any field in the API that is an ID of another object or a collection of API objects (for example, the skill field on Decorations or the skills field on Charms) can be searched as if it were an object. For example, in order to find any decorations whose skill modifies your ice elemental damage, you could use the following query document on the /decorations endpoint.

{
  "skill.ranks.modifiers.damageIce": {
    "$exists": true
  }
}

Operators

An operator starts with a dollar sign, and allows you to perform more than just equality checks on data.

Name Description
$and Joins query clauses with a logical AND.
$or Joins query clauses with a logical OR.
$gt Matches values that are greater than the specified value.
$gte Matches values that are greater than or equal to the specified value.
$lt Matches values that are less than the specified value.
$lte Matches values that are less than or equal to the specified value.
$eq Matches values that are equal to the specified value.
$neq Matches values that are not equal to the specified value.
$in Matches values that are equal to any of the values in the specified array.
$nin Matches values that are not equal to any of the values in the specified array.
$like Matches values that match the provided comparison string.
$nlike Matches values that do not match the provided comparison string.
$exists Matches values that match the given existence check.

$and

Syntax: {"$and": [{expression1}, {expression2}, ..., {expressionN}]}

The $and joins query clauses with a logical AND.

{
  "$and": [
    {
      "rank": "high"
    },
    {
      "attributes.defense": 40
    }
  ]
}

The above query, applied to the /armor endpoint, would find only armor that has a rank equal to "high, and a defense attribute of 40.

By default, consecutive expressions are joined by an $and operator, so in most cases it's not necessary to use $and to join expressions. The above document could be shortened to the following, and would yield the same results.

{
  "rank": "high",
  "attributes.defense": 40
}

$or

Syntax: {"$or": [{expression1}, {expression2}, ..., {expressionN}]}

The $or joins query clauses with a logical OR.

{
  "$or": [
    {
      "rank": "high"
    },
    {
      "attributes.defense": 40
    }
  ]
}

The example above would match any entity where rank = "high" OR attributes.defense = 40.

$gt

Syntax: {"field": {"$gt": value}}

The $gt operator matches values that are greater than the specified value.

{
  "attributes.defense": {
    "$gt": 40
  }
}

The example above would match any entity where attributes.defense > 40.

$gte

Syntax: {"field": {"$gte": value}}

The $gte operator matches values that are greater than or equal to the specified value.

{
  "attributes.defense": {
    "$gte": 40
  }
}

The example above would match any entity where attributes.defense >= 40.

$lt

Syntax: {"field": {"$lt": value}}

The $lt operator changes the field comparison to less than.

{
  "attributes.defense": {
    "$lt": 40
  }
}

The example above would match any entity where attributes.defense < 40.

$lte

Syntax: {"field": {"$lte": value}}

The $lte operator matches values that are less than or equal to the specified value.

{
  "attributes.defense": {
    "$lte": 40
  }
}

The example above would match any entity where attributes.defense <= 40.

$eq

Syntax: {"field": {"$eq": value}} or {"field": value}

The $eq operator matches values that are equal to the specified value.

{
  "attributes.defense": 40
}

The example above would match any entity where attributes.defense = 40.

$neq

Syntax: {"field": {"$neq": value}}

The $neq operator is the negated form of $eq, and matches values that are not equal to the specified value.

{
  "attributes.defense": 40
}

The example above would match any entity where attributes.defense != 40.

$in

Syntax: {"field": {"$in": [value1, value2, ..., valueN]}}

The $in operator matches values that are equal to any value in the specified array.

{
  "rank": {
    "$in": [
      "high",
      "g"
    ]
  }
}

The example above would match any entity where rank IN ("high", "g").

$nin

Syntax: {"field": {"$nin": [value1, value2, ..., valueN]}}

The $nin operator is the negated form of $in, and matches any values that are not equal to any value in the specified array.

{
  "rank": {
    "$nin": [
      "low",
      "high"
    ]
  }
}

The example above would match any entity where rank NOT IN ("low", "high").

$like

Syntax: {"field": {"$like": value}}

The $like operator matches any value that matches the specified comparison string.

A comparison string may contain any character allowable in a normal string, but treats the following characters as metacharacters.

Character Description
% Matches anything zero or more times
_ Matches anything once

To use either character as their literal character and not a metacharacter, simply prefix them with a backslash, like so: \% or \_.

{
  "name": {
    "$like": "Nergal%"
  }
}

The example above would match any entity where the name field begins with the string "Nergal" (name LIKE "Nergal%").

$nlike

Syntax: {"field": {"$nlike": value}}

The $nlike operator is the negated form of $like, and matches any value that does not match the specified comparison string. Please see the documentation for $like for an explanation of comparison strings.

{
  "name": {
    "$nlike": "Nergal%"
  }
}

The example above would match any entity where the name field does not begin with "Nergal" (name NOT LIKE "Nergal%").

$exists

Syntax: {"field": {"$exists": boolean}}

The $exists operator matches subfield existence. When the value of $exists is true, it will match any entity that has the specified field. When it is false, it will match any entity that does not have the specified field.

{
  "attributes.slotsRank2": {
    "$exists": true
  }
}

The example above would match any entity that has the provided field (or, in other words, where attributes.slotsRank2 IS NOT NULL).