Mongo.Ecto.Regex (mongodb_ecto v1.1.2) View Source

An Ecto type to represent MongoDB's Regex objects

Both MongoDB and Elixir use PCRE (Pearl Compatible Regular Expressions), so the syntax is similar for both.

Using in queries

MongoDB supports two ways to use regexes.

One is to use regex object:

regex = Mongo.Ecto.Helpers.regex("elixir", "i")
from p in Post, where: fragment(title: ^regex)

The other one is to use the $regex operator:

from p in Post, where: fragment(title: ["$regex": "elixir", "$options": "i"])

For more information about the differences between the two syntaxes please refer to the MongoDB documentation: http://docs.mongodb.org/manual/reference/operator/query/regex/#syntax-restrictions

Options

  • i - case insensitive
  • m - causes ^ and $ to mark the beginning and end of each line
  • x - whitespace characters are ignored except when escaped and allow # to delimit comments, requires using $regex operator together with the $option operator
  • s - causes dot to match newlines, requires using $regex operator together with the $option operator

Link to this section Summary

Functions

Casts to database format

Callback implementation for Ecto.Type.embed_as/1.

Callback implementation for Ecto.Type.equal?/2.

The Ecto primitive type.

Link to this section Types

Specs

t() :: %Mongo.Ecto.Regex{options: String.t(), pattern: String.t()}

Link to this section Functions