mongox_ecto v0.2.0 Mongo.Ecto.Regex

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

Summary

Functions

Casts to database format

Converts a Mongo.Ecto.Regex into BSON.Regex

Converts a BSON.Regex into Mongo.Ecto.Regex

The Ecto primitive type

Types

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

Functions

cast(js)

Casts to database format

dump(js)

Converts a Mongo.Ecto.Regex into BSON.Regex

load(js)

Converts a BSON.Regex into Mongo.Ecto.Regex

type()

The Ecto primitive type.