Mongo.Ecto.Helpers

Defines helpers to ease working with MongoDB in models or other places where you work with them. You’d probably want to import it.

Source

Summary

change_array(idx, field \\ "", value)

Allows updating only a fragment of a nested document inside an array

change_map(field, value)

Allows updating only a fragment of a nested document

javascript(code, scope \\ [])

Allows using inline JavaScript in queries in where clauses and inserting it as a value to the database

regex(pattern, options \\ "")

Creates proper regex object that can be passed to the database

Functions

change_array(idx, field \\ "", value)

Specs:

Allows updating only a fragment of a nested document inside an array

Usage in queries

MyRepo.update_all(Post,
  set: [comments: change_array(0, "author", "NewName")])
Source
change_map(field, value)

Specs:

Allows updating only a fragment of a nested document

Usage in queries

MyRepo.update_all(Post,
  set: [meta: change_map("author.name", "NewName")])
Source
javascript(code, scope \\ [])

Specs:

Allows using inline JavaScript in queries in where clauses and inserting it as a value to the database.

The second argument acts as a context for the function. All values will be converted to valid BSON types.

Raises ArgumentError if any of the values cannot be converted.

Usage in queries

from p in Post,
  where: ^javascript("this.value === value", value: 1)
Source
regex(pattern, options \\ "")

Specs:

Creates proper regex object that can be passed to the database.

Usage in queries

from p in Post,
  where: fragment(title: ^regex("elixir", "i"))

For supported options please see Mongo.Ecto.Regex module documentation.

Source