ReferencesMScript Query Reference
References

MScript Query Operators Reference

Comprehensive reference for all MScript Query operators with their Sequelize, MongoDB, and Elasticsearch equivalents, including examples for each operator.

MScript Query provides a unified query syntax that works across MongoDB, Elasticsearch, and Sequelize (PostgreSQL, MySQL, etc.). This document provides a complete reference for all available operators, their platform-specific equivalents, and usage examples.


Basic Comparison Operators

$eq - Equals

Description: Matches values that are equal to the specified value.

MScript Query:

{
  status: { $eq: "active" }
}

Shorthand: You can omit $eq for simple equality:

{
  status: "active"
}

Platform Equivalents:

Sequelize

{
  status: "active"
}
// or
{
  status: { [Op.eq]: "active" }
}

MongoDB

{
  status: "active"
}

Elasticsearch

{
  term: {
    status: {
      value: "active"
    }
  }
}

Example:

{
  role: { $eq: "admin" }
}

$ne - Not Equals

Description: Matches values that are not equal to the specified value.

MScript Query:

{
  status: { $ne: "deleted" }
}

Platform Equivalents:

Sequelize

{
  status: {
    [Op.ne]: "deleted"
  }
}

MongoDB

{
  status: {
    $ne: "deleted"
  }
}

Elasticsearch

{
  bool: {
    must_not: {
      term: {
        status: {
          value: "deleted"
        }
      }
    }
  }
}

Example:

{
  isActive: { $ne: false }
}

$gt - Greater Than

Description: Matches values that are greater than the specified value.

MScript Query:

{
  age: { $gt: 18 }
}

Platform Equivalents:

Sequelize

{
  age: {
    [Op.gt]: 18
  }
}

MongoDB

{
  age: {
    $gt: 18
  }
}

Elasticsearch

{
  range: {
    age: {
      gt: 18
    }
  }
}

Example:

{
  price: { $gt: 100.5 }
}

$gte - Greater Than or Equal

Description: Matches values that are greater than or equal to the specified value.

MScript Query:

{
  age: { $gte: 18 }
}

Platform Equivalents:

Sequelize

{
  age: {
    [Op.gte]: 18
  }
}

MongoDB

{
  age: {
    $gte: 18
  }
}

Elasticsearch

{
  range: {
    age: {
      gte: 18
    }
  }
}

Example:

{
  minimumOrder: { $gte: 50 }
}

$lt - Less Than

Description: Matches values that are less than the specified value.

MScript Query:

{
  age: { $lt: 65 }
}

Platform Equivalents:

Sequelize

{
  age: {
    [Op.lt]: 65
  }
}

MongoDB

{
  age: {
    $lt: 65
  }
}

Elasticsearch

{
  range: {
    age: {
      lt: 65
    }
  }
}

Example:

{
  discount: { $lt: 0.5 }
}

$lte - Less Than or Equal

Description: Matches values that are less than or equal to the specified value.

MScript Query:

{
  age: { $lte: 65 }
}

Platform Equivalents:

Sequelize

{
  age: {
    [Op.lte]: 65
  }
}

MongoDB

{
  age: {
    $lte: 65
  }
}

Elasticsearch

{
  range: {
    age: {
      lte: 65
    }
  }
}

Example:

{
  maxPrice: { $lte: 1000 }
}

$in - In List

Description: Matches any of the values specified in an array.

MScript Query:

{
  role: { $in: ["admin", "user", "moderator"] }
}

Platform Equivalents:

Sequelize

{
  role: {
    [Op.in]: ["admin", "user", "moderator"]
  }
}

MongoDB

{
  role: {
    $in: ["admin", "user", "moderator"]
  }
}

Elasticsearch

{
  terms: {
    role: ["admin", "user", "moderator"]
  }
}

Example:

{
  status: { $in: ["pending", "processing", "completed"] }
}

Note: You can also use array shorthand:

{
  role: ["admin", "user"]
}

$nin - Not In List

Description: Matches none of the values specified in an array.

MScript Query:

{
  role: { $nin: ["guest", "banned"] }
}

Platform Equivalents:

Sequelize

{
  role: {
    [Op.notIn]: ["guest", "banned"]
  }
}

MongoDB

{
  role: {
    $nin: ["guest", "banned"]
  }
}

Elasticsearch

{
  bool: {
    must_not: {
      terms: {
        role: ["guest", "banned"]
      }
    }
  }
}

Example:

{
  category: { $nin: ["deleted", "archived"] }
}

Pattern Matching Operators

$like - Like Pattern (Case-Sensitive)

Description: Matches strings using SQL LIKE pattern matching (case-sensitive). Use % as wildcard.

MScript Query:

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

Platform Equivalents:

Sequelize

{
  name: {
    [Op.like]: "%john%"
  }
}

MongoDB (Note: MongoDB regex is case-insensitive by default)

{
  name: {
    $regex: /john/,
    $options: "i"
  }
}

Elasticsearch

{
  wildcard: {
    name: "*john*"
  }
}

Example:

{
  email: { $like: "%@gmail.com" }
}

$ilike - Like Pattern (Case-Insensitive)

Description: Matches strings using SQL LIKE pattern matching (case-insensitive). Use % as wildcard.

MScript Query:

{
  name: { $ilike: "%john%" }
}

Platform Equivalents:

Sequelize

{
  name: {
    [Op.iLike]: "%john%"
  }
}

MongoDB

{
  name: {
    $regex: /john/i
  }
}

Elasticsearch

{
  wildcard: {
    name: {
      value: "*john*",
      case_insensitive: true
    }
  }
}

Example:

{
  company: { $ilike: "%tech%" }
}

$nlike - Not Like Pattern

Description: Matches strings that do not match the SQL LIKE pattern (case-sensitive).

MScript Query:

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

Platform Equivalents:

Sequelize

{
  name: {
    [Op.notLike]: "%bot%"
  }
}

MongoDB

{
  name: {
    $not: {
      $regex: /bot/
    }
  }
}

Elasticsearch

{
  bool: {
    must_not: {
      wildcard: {
        name: "*bot*"
      }
    }
  }
}

Example:

{
  email: { $nlike: "%test%" }
}

$nilike - Not ILike Pattern

Description: Matches strings that do not match the SQL LIKE pattern (case-insensitive).

MScript Query:

{
  name: { $nilike: "%bot%" }
}

Platform Equivalents:

Sequelize

{
  name: {
    [Op.notILike]: "%bot%"
  }
}

MongoDB

{
  name: {
    $not: {
      $regex: /bot/i
    }
  }
}

Elasticsearch

{
  bool: {
    must_not: {
      wildcard: {
        name: {
          value: "*bot*",
          case_insensitive: true
        }
      }
    }
  }
}

Example:

{
  domain: { $nilike: "%spam%" }
}

$match - Match Regex

Description: Matches strings using regular expression pattern.

MScript Query:

{
  description: { $match: "sale.*discount" }
}

Platform Equivalents:

Sequelize

{
  description: {
    [Op.match]: "sale.*discount"
  }
}

MongoDB

{
  description: {
    $regex: "sale.*discount"
  }
}

Elasticsearch

{
  match: {
    description: "sale.*discount"
  }
}

Example:

{
  phone: { $match: "^\\+1[0-9]{10}$" }
}

$nmatch - Not Match Regex

Description: Matches strings that do not match the regular expression pattern.

MScript Query:

{
  description: { $nmatch: "error.*failed" }
}

Platform Equivalents:

Sequelize

{
  description: {
    [Op.notMatch]: "error.*failed"
  }
}

MongoDB

{
  description: {
    $not: {
      $regex: "error.*failed"
    }
  }
}

Elasticsearch

{
  bool: {
    must_not: {
      match: {
        description: "error.*failed"
      }
    }
  }
}

Example:

{
  code: { $nmatch: "^ERROR" }
}

Range and Null Check Operators

$between - Value Between Range

Description: Matches values that fall within the specified range (inclusive).

MScript Query:

{
  price: { $between: [10, 100] }
}

Platform Equivalents:

Sequelize

{
  price: {
    [Op.between]: [10, 100]
  }
}

MongoDB

{
  price: {
    $gte: 10,
    $lte: 100
  }
}

Elasticsearch

{
  range: {
    price: {
      gte: 10,
      lte: 100
    }
  }
}

Example:

{
  age: { $between: [18, 65] }
}

$nbetween - Value Not In Range

Description: Matches values that fall outside the specified range.

MScript Query:

{
  price: { $nbetween: [10, 100] }
}

Platform Equivalents:

Sequelize

{
  price: {
    [Op.notBetween]: [10, 100]
  }
}

MongoDB

{
  $or: [
    { price: { $lt: 10 } },
    { price: { $gt: 100 } }
  ]
}

Elasticsearch

{
  bool: {
    must_not: {
      range: {
        price: {
          gte: 10,
          lte: 100
        }
      }
    }
  }
}

Example:

{
  score: { $nbetween: [0, 100] }
}

$isnull - Is Null

Description: Matches fields that are null or do not exist.

MScript Query:

{
  deletedAt: { $isnull: true }
}

Platform Equivalents:

Sequelize

{
  deletedAt: {
    [Op.is]: null
  }
}

MongoDB

{
  deletedAt: null
}

Elasticsearch

{
  bool: {
    must_not: {
      exists: {
        field: "deletedAt"
      }
    }
  }
}

Example:

{
  optionalField: { $isnull: true }
}

$notnull - Is Not Null

Description: Matches fields that are not null and exist.

MScript Query:

{
  deletedAt: { $notnull: true }
}

Platform Equivalents:

Sequelize

{
  deletedAt: {
    [Op.not]: null
  }
}

MongoDB

{
  deletedAt: {
    $ne: null
  }
}

Elasticsearch

{
  exists: {
    field: "deletedAt"
  }
}

Example:

{
  email: { $notnull: true }
}

Array and String Operations

$contains - Array Contains Value

Description: Matches arrays that contain the specified value.

MScript Query:

{
  tags: { $contains: "news" }
}

Platform Equivalents:

Sequelize (PostgreSQL array operator)

{
  tags: {
    [Op.contains]: ["news"]
  }
}

MongoDB

{
  tags: {
    $elemMatch: {
      $eq: "news"
    }
  }
}

Elasticsearch

{
  match: {
    tags: "news"
  }
}

Example:

{
  categories: { $contains: "technology" }
}

$ncontains - Array Does Not Contain Value

Description: Matches arrays that do not contain the specified value.

MScript Query:

{
  tags: { $ncontains: "draft" }
}

Platform Equivalents:

Sequelize

{
  tags: {
    [Op.notContains]: ["draft"]
  }
}

MongoDB

{
  tags: {
    $not: {
      $elemMatch: {
        $eq: "draft"
      }
    }
  }
}

Elasticsearch

{
  bool: {
    must_not: {
      match: {
        tags: "draft"
      }
    }
  }
}

Example:

{
  excludedTags: { $ncontains: "archived" }
}

$overlap - Arrays Have Common Elements

Description: Matches arrays that have at least one element in common with the specified array.

MScript Query:

{
  tags: { $overlap: ["tech", "ai"] }
}

Platform Equivalents:

Sequelize (PostgreSQL array operator)

{
  tags: {
    [Op.overlap]: ["tech", "ai"]
  }
}

MongoDB

{
  tags: {
    $elemMatch: {
      $in: ["tech", "ai"]
    }
  }
}

Elasticsearch

{
  match: {
    tags: ["tech", "ai"]
  }
}

Example:

{
  skills: { $overlap: ["javascript", "python"] }
}

$noverlap - Arrays Have No Common Elements

Description: Matches arrays that have no elements in common with the specified array.

MScript Query:

{
  tags: { $noverlap: ["old", "deprecated"] }
}

Platform Equivalents:

Sequelize

{
  [Op.not]: {
    tags: {
      [Op.overlap]: ["old", "deprecated"]
    }
  }
}

MongoDB

{
  tags: {
    $not: {
      $elemMatch: {
        $in: ["old", "deprecated"]
      }
    }
  }
}

Elasticsearch

{
  bool: {
    must_not: {
      match: {
        tags: ["old", "deprecated"]
      }
    }
  }
}

Example:

{
  categories: { $noverlap: ["deleted", "archived"] }
}

$starts - Starts With String

Description: Matches strings that start with the specified value.

MScript Query:

{
  name: { $starts: "Mr." }
}

Platform Equivalents:

Sequelize

{
  name: {
    [Op.startsWith]: "Mr."
  }
}

MongoDB

{
  name: {
    $regex: /^Mr\./
  }
}

Elasticsearch

{
  prefix: {
    name: "Mr."
  }
}

Example:

{
  email: { $starts: "admin@" }
}

$nstarts - Does Not Start With String

Description: Matches strings that do not start with the specified value.

MScript Query:

{
  name: { $nstarts: "Ms." }
}

Platform Equivalents:

Sequelize

{
  name: {
    [Op.notStartsWith]: "Ms."
  }
}

MongoDB

{
  name: {
    $not: {
      $regex: /^Ms\./
    }
  }
}

Elasticsearch

{
  bool: {
    must_not: {
      prefix: {
        name: "Ms."
      }
    }
  }
}

Example:

{
  path: { $nstarts: "/admin" }
}

$ends - Ends With String

Description: Matches strings that end with the specified value.

MScript Query:

{
  email: { $ends: "@gmail.com" }
}

Platform Equivalents:

Sequelize

{
  email: {
    [Op.endsWith]: "@gmail.com"
  }
}

MongoDB

{
  email: {
    $regex: /@gmail\.com$/
  }
}

Elasticsearch

{
  wildcard: {
    email: "*@gmail.com"
  }
}

Example:

{
  filename: { $ends: ".pdf" }
}

$nends - Does Not End With String

Description: Matches strings that do not end with the specified value.

MScript Query:

{
  email: { $nends: "@yahoo.com" }
}

Platform Equivalents:

Sequelize

{
  email: {
    [Op.notEndsWith]: "@yahoo.com"
  }
}

MongoDB

{
  email: {
    $not: {
      $regex: /@yahoo\.com$/
    }
  }
}

Elasticsearch

{
  bool: {
    must_not: {
      wildcard: {
        email: "*@yahoo.com"
      }
    }
  }
}

Example:

{
  url: { $nends: ".exe" }
}

Logical Operators

$and - All Conditions Must Be True

Description: Combines multiple conditions with AND logic. All conditions must be true.

MScript Query:

{
  $and: [
    { status: "active" },
    { age: { $gte: 18 } }
  ]
}

Platform Equivalents:

Sequelize

{
  [Op.and]: [
    { status: "active" },
    {
      age: {
        [Op.gte]: 18
      }
    }
  ]
}

MongoDB

{
  $and: [
    { status: "active" },
    {
      age: {
        $gte: 18
      }
    }
  ]
}

Elasticsearch

{
  bool: {
    must: [
      {
        term: {
          status: {
            value: "active"
          }
        }
      },
      {
        range: {
          age: {
            gte: 18
          }
        }
      }
    ]
  }
}

Example:

{
  $and: [
    { isPublished: true },
    { views: { $gte: 100 } },
    { tags: { $contains: "featured" } }
  ]
}

Note: Multiple fields at the same level are automatically combined with $and:

{
  status: "active",
  age: { $gte: 18 }
}

This is equivalent to the $and example above.


$or - At Least One Condition Is True

Description: Combines multiple conditions with OR logic. At least one condition must be true.

MScript Query:

{
  $or: [
    { status: "pending" },
    { status: "processing" }
  ]
}

Platform Equivalents:

Sequelize

{
  [Op.or]: [
    { status: "pending" },
    { status: "processing" }
  ]
}

MongoDB

{
  $or: [
    { status: "pending" },
    { status: "processing" }
  ]
}

Elasticsearch

{
  bool: {
    should: [
      {
        term: {
          status: {
            value: "pending"
          }
        }
      },
      {
        term: {
          status: {
            value: "processing"
          }
        }
      }
    ]
  }
}

Example:

{
  $or: [
    { views: { $gte: 1000 } },
    { likes: { $gte: 500 } }
  ]
}

$not - Negates a Condition

Description: Negates the specified condition.

MScript Query:

{
  $not: {
    status: "deleted"
  }
}

Platform Equivalents:

Sequelize

{
  [Op.not]: {
    status: "deleted"
  }
}

MongoDB

{
  $not: {
    status: "deleted"
  }
}

Elasticsearch

{
  bool: {
    must_not: {
      term: {
        status: {
          value: "deleted"
        }
      }
    }
  }
}

Example:

{
  $not: {
    role: { $in: ["guest", "banned"] }
  }
}

$nor - None of the Conditions Are True

Description: Matches documents where none of the specified conditions are true.

MScript Query:

{
  $nor: [
    { status: "deleted" },
    { isArchived: true }
  ]
}

Platform Equivalents:

Sequelize

{
  [Op.not]: [
    { status: "deleted" },
    { isArchived: true }
  ]
}

MongoDB

{
  $nor: [
    { status: "deleted" },
    { isArchived: true }
  ]
}

Elasticsearch

{
  bool: {
    must_not: [
      {
        term: {
          status: {
            value: "deleted"
          }
        }
      },
      {
        term: {
          isArchived: {
            value: true
          }
        }
      }
    ]
  }
}

Example:

{
  $nor: [
    { role: "guest" },
    { isBanned: true }
  ]
}

MongoDB-Specific Operators

These operators are primarily designed for MongoDB but may have limited support in other platforms.

$exists - Field Exists

Description: Matches documents that have the specified field (MongoDB-specific).

MScript Query:

{
  optionalField: { $exists: true }
}

Platform Equivalents:

Sequelize (similar behavior)

{
  optionalField: {
    [Op.not]: null
  }
}

MongoDB

{
  optionalField: {
    $exists: true
  }
}

Elasticsearch

{
  exists: {
    field: "optionalField"
  }
}

Example:

{
  metadata: { $exists: true }
}

$nexists - Field Does Not Exist

Description: Matches documents that do not have the specified field (MongoDB-specific).

MScript Query:

{
  deletedAt: { $nexists: true }
}

Platform Equivalents:

Sequelize (similar behavior)

{
  deletedAt: {
    [Op.is]: null
  }
}

MongoDB

{
  deletedAt: {
    $exists: false
  }
}

Elasticsearch

{
  bool: {
    must_not: {
      exists: {
        field: "deletedAt"
      }
    }
  }
}

Example:

{
  tempField: { $nexists: true }
}

$all - Array Contains All Values

Description: Matches arrays that contain all of the specified values (MongoDB-specific).

MScript Query:

{
  tags: { $all: ["tech", "ai", "machine-learning"] }
}

Platform Equivalents:

Sequelize (PostgreSQL)

{
  tags: {
    [Op.contains]: ["tech", "ai", "machine-learning"]
  }
}

MongoDB

{
  tags: {
    $all: ["tech", "ai", "machine-learning"]
  }
}

Elasticsearch

{
  match: {
    tags: {
      query: "tech ai machine-learning",
      operator: "and"
    }
  }
}

Example:

{
  requiredSkills: { $all: ["javascript", "react", "nodejs"] }
}

$notall - Array Does Not Contain All Values

Description: Matches arrays that do not contain all of the specified values (MongoDB-specific).

MScript Query:

{
  tags: { $notall: ["premium", "featured"] }
}

Platform Equivalents:

Sequelize

{
  [Op.not]: {
    tags: {
      [Op.contains]: ["premium", "featured"]
    }
  }
}

MongoDB

{
  tags: {
    $not: {
      $all: ["premium", "featured"]
    }
  }
}

Elasticsearch

{
  bool: {
    must_not: {
      match: {
        tags: {
          query: "premium featured",
          operator: "and"
        }
      }
    }
  }
}

Example:

{
  skills: { $notall: ["expert", "certified"] }
}

$size - Array Size

Description: Matches arrays with exactly the specified number of elements (MongoDB-specific).

MScript Query:

{
  tags: { $size: 3 }
}

Platform Equivalents:

Sequelize (PostgreSQL)

Sequelize.where(
  Sequelize.fn("array_length", Sequelize.col("tags"), 1),
  3
)

MongoDB

{
  tags: {
    $size: 3
  }
}

Elasticsearch

{
  script: {
    script: {
      source: "doc['tags'].size() == 3"
    }
  }
}

Example:

{
  items: { $size: 5 }
}

$notsize - Array Size Not Equal

Description: Matches arrays that do not have the specified number of elements (MongoDB-specific).

MScript Query:

{
  tags: { $notsize: 0 }
}

Platform Equivalents:

Sequelize

Sequelize.where(
  Sequelize.fn("array_length", Sequelize.col("tags"), 1),
  { [Op.ne]: 0 }
)

MongoDB

{
  tags: {
    $not: {
      $size: 0
    }
  }
}

Elasticsearch

{
  bool: {
    must_not: {
      script: {
        script: {
          source: "doc['tags'].size() == 0"
        }
      }
    }
  }
}

Example:

{
  items: { $notsize: 1 }
}

$any - Array Contains Any Value

Description: Matches arrays that contain any of the specified values (MongoDB-specific, similar to $overlap).

MScript Query:

{
  tags: { $any: ["tech", "ai"] }
}

Platform Equivalents:

Sequelize (Note: Op.overlap is Sequelize.Op.overlap, PostgreSQL)

{
  tags: {
    [Op.overlap]: ["tech", "ai"]
  }
}

MongoDB

{
  tags: {
    $elemMatch: {
      $in: ["tech", "ai"]
    }
  }
}

Elasticsearch

{
  match: {
    tags: ["tech", "ai"]
  }
}

Example:

{
  categories: { $any: ["news", "blog", "article"] }
}

$notany - Array Does Not Contain Any Value

Description: Matches arrays that do not contain any of the specified values (MongoDB-specific).

MScript Query:

{
  tags: { $notany: ["deleted", "archived"] }
}

Platform Equivalents:

Sequelize

{
  [Op.not]: {
    tags: {
      [Op.overlap]: ["deleted", "archived"]
    }
  }
}

MongoDB

{
  tags: {
    $not: {
      $elemMatch: {
        $in: ["deleted", "archived"]
      }
    }
  }
}

Elasticsearch

{
  bool: {
    must_not: {
      match: {
        tags: ["deleted", "archived"]
      }
    }
  }
}

Example:

{
  excludedCategories: { $notany: ["spam", "test"] }
}

Geographic Operators

$geoNear - Geographic Distance

Description: Finds documents near a geographic point within a specified distance.

MScript Query:

{
  location: {
    $geoNear: {
      point: {
        type: "Point",
        coordinates: [-122.4194, 37.7749]
      },
      maxDistance: 5000,
      minDistance: 100
    }
  }
}

Platform Equivalents:

Sequelize

Sequelize.where(
  Sequelize.fn(
    "ST_DWithin",
    Sequelize.col("location"),
    Sequelize.literal(
      "ST_SetSRID(ST_MakePoint(-122.4194, 37.7749),4326)::geography"
    ),
    5000
  ),
  true
)

MongoDB

{
  location: {
    $nearSphere: {
      $geometry: {
        type: "Point",
        coordinates: [-122.4194, 37.7749]
      },
      $maxDistance: 5000,
      $minDistance: 100
    }
  }
}

Elasticsearch

{
  geo_distance: {
    distance: "5000m",
    location: {
      lon: -122.4194,
      lat: 37.7749
    }
  }
}

Example:

{
  coordinates: {
    $geoNear: {
      point: {
        type: "Point",
        coordinates: [28.9784, 41.0082]
      },
      maxDistance: 10000
    }
  }
}

Note:

  • point must be a GeoJSON Point object with type: "Point" and coordinates: [longitude, latitude]
  • maxDistance is required and specified in meters
  • minDistance is optional and specified in meters

$geoWithin - Geographic Containment

Description: Finds documents within a geographic boundary (polygon or bounding box).

MScript Query (with bounding box):

{
  location: {
    $geoWithin: {
      bbox: [-122.5, 37.7, -122.3, 37.8]
    }
  }
}

MScript Query (with GeoJSON shape):

{
  location: {
    $geoWithin: {
      shape: {
        type: "Polygon",
        coordinates: [[
          [-122.5, 37.7],
          [-122.3, 37.7],
          [-122.3, 37.8],
          [-122.5, 37.8],
          [-122.5, 37.7]
        ]]
      }
    }
  }
}

Platform Equivalents:

Sequelize

Sequelize.where(
  Sequelize.fn(
    "ST_Within",
    Sequelize.literal('("location")::geometry'),
    Sequelize.literal("ST_MakeEnvelope(-122.5, 37.7, -122.3, 37.8, 4326)")
  ),
  true
)

MongoDB

// Bounding box
{
  location: {
    $geoWithin: {
      $box: [
        [-122.5, 37.7],
        [-122.3, 37.8]
      ]
    }
  }
}

// GeoJSON polygon (placeholder coordinates)
{
  location: {
    $geoWithin: {
      $geometry: {
        type: "Polygon",
        coordinates: [
          /* ... */
        ]
      }
    }
  }
}

Elasticsearch

// Bounding box
{
  geo_bounding_box: {
    location: {
      top_left: {
        lon: -122.5,
        lat: 37.8
      },
      bottom_right: {
        lon: -122.3,
        lat: 37.7
      }
    }
  }
}

// Polygon (placeholder points)
{
  geo_polygon: {
    location: {
      points: [
        { lon: -122.5, lat: 37.7 }
        /* ... */
      ]
    }
  }
}

Example:

{
  coordinates: {
    $geoWithin: {
      bbox: [28.9, 41.0, 29.0, 41.1]
    }
  }
}

Note:

  • bbox format: [minLon, minLat, maxLon, maxLat]
  • shape must be a valid GeoJSON Polygon or MultiPolygon

$geoContains - Geographic Contains

Description: Finds documents where the field (polygon/area) contains the specified point or shape.

MScript Query:

{
  serviceArea: {
    $geoContains: {
      shape: {
        type: "Point",
        coordinates: [-122.4194, 37.7749]
      }
    }
  }
}

Platform Equivalents:

Sequelize

Sequelize.where(
  Sequelize.fn(
    "ST_Contains",
    Sequelize.literal('("serviceArea")::geometry'),
    Sequelize.literal(
      "ST_SetSRID(ST_GeomFromGeoJSON('{"type":"Point","coordinates":[-122.4194,37.7749]}'),4326)"
    )
  ),
  true
)

MongoDB

{
  serviceArea: {
    $geoIntersects: {
      $geometry: {
        type: "Point",
        coordinates: [-122.4194, 37.7749]
      }
    }
  }
}

Elasticsearch

{
  geo_shape: {
    serviceArea: {
      shape: {
        type: "Point",
        coordinates: [-122.4194, 37.7749]
      },
      relation: "contains"
    }
  }
}

Example:

{
  deliveryZone: {
    $geoContains: {
      shape: {
        type: "Point",
        coordinates: [28.9784, 41.0082]
      }
    }
  }
}

Note: The field must be a geographic polygon/area type. The shape can be a Point, Polygon, or other GeoJSON geometry.


$geoIntersects - Geographic Intersection

Description: Finds documents where the field intersects with the specified shape.

MScript Query:

{
  route: {
    $geoIntersects: {
      shape: {
        type: "Polygon",
        coordinates: [[
          [-122.5, 37.7],
          [-122.3, 37.7],
          [-122.3, 37.8],
          [-122.5, 37.8],
          [-122.5, 37.7]
        ]]
      }
    }
  }
}

Platform Equivalents:

Sequelize

Sequelize.where(
  Sequelize.fn(
    "ST_Intersects",
    Sequelize.literal('("route")::geometry'),
    Sequelize.literal(
      "ST_SetSRID(ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[/* ... */]}'),4326)"
    )
  ),
  true
)

MongoDB

{
  route: {
    $geoIntersects: {
      $geometry: {
        type: "Polygon",
        coordinates: [
          /* ... */
        ]
      }
    }
  }
}

Elasticsearch

{
  geo_shape: {
    route: {
      shape: {
        type: "Polygon",
        coordinates: [
          /* ... */
        ]
      },
      relation: "intersects"
    }
  }
}

Example:

{
  boundary: {
    $geoIntersects: {
      shape: {
        type: "LineString",
        coordinates: [[28.9, 41.0], [29.0, 41.1]]
      }
    }
  }
}

Note: The shape can be any GeoJSON geometry type (Point, LineString, Polygon, etc.).


Complex Query Examples

Combining Multiple Operators

{
  $and: [
    { status: "active" },
    {
      $or: [
        { age: { $gte: 18 } },
        { isVerified: true }
      ]
    },
    { tags: { $overlap: ["premium", "featured"] } },
    { email: { $ilike: "%@gmail.com" } }
  ]
}

Nested Logical Operators

{
  $and: [
    { isPublished: true },
    {
      $or: [
        { views: { $gte: 1000 } },
        {
          $and: [
            { likes: { $gte: 500 } },
            { comments: { $gte: 100 } }
          ]
        }
      ]
    },
    { deletedAt: { $isnull: true } }
  ]
}

Geographic Search with Filters

{
  $and: [
    {
      location: {
        $geoNear: {
          point: {
            type: "Point",
            coordinates: [-122.4194, 37.7749]
          },
          maxDistance: 5000
        }
      }
    },
    { isActive: true },
    { category: { $in: ["restaurant", "cafe"] } }
  ]
}

Platform-Specific Notes

Sequelize (PostgreSQL/MySQL)

  • Array operators ($contains, $overlap) work best with PostgreSQL array columns
  • Geographic operators require PostGIS extension for PostgreSQL
  • $ilike is PostgreSQL-specific; MySQL uses case-insensitive collation
  • $match uses database-specific regex syntax

MongoDB

  • Native support for all operators
  • Geographic operators require geospatial indexes
  • Array operators work with MongoDB arrays
  • $exists and $nexists are MongoDB-native concepts

Elasticsearch

  • Uses Elasticsearch Query DSL
  • Geographic queries require geo_point or geo_shape field mappings
  • Array fields are typically analyzed as text, so array operators may behave differently
  • Pattern matching uses Elasticsearch wildcard or match queries

Best Practices

  1. Use $ilike instead of $like for case-insensitive searches when possible
  2. Combine conditions explicitly with $and when logic is complex
  3. Use $in for multiple exact matches instead of multiple $or conditions
  4. Leverage $between for range queries instead of combining $gte and $lte
  5. Use $isnull/$notnull for null checks instead of $eq: null or $ne: null
  6. Index fields used in queries for better performance
  7. Test geographic queries carefully as coordinate systems and precision vary by platform