Navigation

Field Definition

Field Types

Even though MongoDB is a schemaless database and allows data to be stored as strings, Mongoid permits the application to declare the type of data stored in the various fields of a document. Field type declarations affect the following:

1. When assigning values to fields, the values are converted to the specified type.

2. When persisting data to MongoDB, the data is sent in an appropriate type, permitting richer data manipulation within MongoDB or by other tools.

3. When querying documents, query parameters are converted to the specified type before being sent to MongoDB.

4. When retrieving documents from the database, field values are converted to the specified type.

Field type definitions determine how Mongoid behaves when constructing the queries, retrieving and writing fields from the database. Changing the field definitions in a model class does not alter data already stored in MongoDB. To update type or contents of fields of existing documents, the field must be re-saved to the database. Note that, due to Mongoid tracking which attributes on a model change and only saving the changed ones, it may be necessary to explicitly write a field value when changing the type of an existing field without changing the stored values.

Consider a simple class for modeling a person in an application. A person may have a first name, last name, and middle name. We can define these attributes on a person by using the field macro.

class Person
  include Mongoid::Document
  field :first_name, type: String
  field :middle_name, type: String
  field :last_name, type: String
end

Below is a list of valid types for fields.

  • Array
  • BigDecimal
  • Boolean
  • Date
  • DateTime
  • Float
  • Hash
  • Integer
  • BSON::ObjectId
  • BSON::Binary
  • Range
  • Regexp
  • Set
  • String
  • StringifiedSymbol
  • Symbol
  • Time
  • TimeWithZone

If you decide not to specify the type of field with the definition, Mongoid will treat it as an object and not try to typecast it when sending the values to the database. This can be advantageous as the lack of attempted conversion will yield a slight performance gain. However some types are not supported if not defined as fields. You can safely omit type specifications when:

  • You’re not using a web front end and values are already properly cast.
  • All of your fields are strings.
class Person
  include Mongoid::Document
  field :first_name
  field :middle_name
  field :last_name
end

Types that are not supported as dynamic attributes since they cannot be cast are:

  • BigDecimal
  • Date
  • DateTime
  • Range

The StringifiedSymbol Type

The StringifiedSymbol field type is the recommended field type for storing values that should be exposed as symbols to Ruby applications. When using the Symbol field type, Mongoid defaults to storing values as BSON symbols. For more information on the BSON symbol type, see here. However, the BSON symbol type is deprecated and is difficult to work with in programming languages without native symbol types, so the StringifiedSymbol type allows the use of symbols while ensuring interoperability with other drivers. The StringifiedSymbol type stores all data on the database as strings, while exposing values to the application as symbols.

An example usage is shown below:

class Post
  include Mongoid::Document

  field :status, type: StringifiedSymbol
end

post = Post.new(status: :hello)
# status is stored as "hello" on the database, but returned as a Symbol
post.status
# => :hello

# String values can be assigned also:
post = Post.new(status: "hello")
# status is stored as "hello" on the database, but returned as a Symbol
post.status
# => :hello

All non-string values will be stringified upon being sent to the database (via to_s), and all values will be converted to symbols when returned to the application. Values that cannot be converted directly to symbols, such as integers and arrays, will first be converted to strings and then symbols before being returned to the application.

For example, setting an integer as status:

post = Post.new(status: 42)
post.status
# => :"42"

If the StringifiedSymbol type is applied to a field that contains BSON symbols, the values will be stored as strings instead of BSON symbols on the next save. This permits transparent lazy migration from fields that currently store either strings or BSON symbols in the database to the StringifiedSymbol field type.

BSON Symbol Type

New applications should use the StringifiedSymbol field type to store Ruby symbols in the database. The StringifiedSymbol field type provides maximum compatibility with other applications and programming languages and has the same behavior in all circumstances.

Mongoid also provides the deprecated Symbol field type for serializing Ruby symbols to BSON symbols. Because the BSON specification deprecated the BSON symbol type, the bson gem will serialize Ruby symbols into BSON strings when used on its own. However, in order to maintain backwards compatibility with older datasets, the mongo gem overrides this behavior to serialize Ruby symbols as BSON symbols. This is necessary to be able to specify queries for documents which contain BSON symbols as fields.

To override the default behavior and configure the mongo gem (and thereby Mongoid as well) to encode symbol values as strings, include the following code snippet in your project:

class Symbol
  def bson_type
    BSON::String::BSON_TYPE
  end
end

Hash Fields

When using a field of type Hash, be wary of adhering to the legal key names for mongoDB, or else the values will not store properly.

class Person
  include Mongoid::Document
  field :first_name
  field :url, type: Hash

  # will update the fields properly and save the values
  def set_vals
    self.first_name = 'Daniel'
    self.url = {'home_page' => 'http://www.homepage.com'}
    save
  end

  # all data will fail to save due to the illegal hash key
  def set_vals_fail
    self.first_name = 'Daniel'
    self.url = {'home.page' => 'http://www.homepage.com'}
    save
  end
end

Time Fields

Time fields store values as Time instances in the configured time zone.

Date and DateTime instances are converted to Time instances upon assignment to a Time field:

class Voter
  include Mongoid::Document

  field :registered_at, type: Time
end

Voter.new(registered_at: Date.today)
# => #<Voter _id: 5fdd80392c97a618f07ba344, registered_at: 2020-12-18 05:00:00 UTC>

In the above example, the value was interpreted as the beginning of today in local time, because the application was not configured to use UTC times.

Date Fields

Mongoid allows assignment of values of several types to Date fields:

  • Date - the provided date is stored as is.
  • Time, DateTime, ActiveSupport::TimeWithZone - the date component of the value is taken in the value’s time zone.
  • String - the date specified in the string is used.
  • Integer, Float - the value is taken to be a UTC timestamp which is converted to the configured time zone (note that Mongoid.use_utc has no effect on this conversion), then the date is taken from the resulting time.

In other words, if a date is specified in the value, that date is used without first converting the value to the configured time zone.

As a date & time to date conversion is lossy (it discards the time component), especially if an application operates with times in different time zones it is recommended to explicitly convert String, Time and DateTime objects to Date objects before assigning the values to fields of type Date.

DateTime Fields

MongoDB stores all times as UTC timestamps. When assigning a value to a DateTime field, or when querying a DateTime field, Mongoid converts the passed in value to a UTC Time before sending it to the MongoDB server.

Time, ActiveSupport::TimeWithZone and DateTime objects embed time zone information, and the value persisted is the specified moment in time, in UTC. When the value is retrieved, the time zone in which it is returned is defined by the configured time zone settings.

class Ticket
  include Mongoid::Document
  field :opened_at, type: DateTime
end

Mongoid.use_activesupport_time_zone = true
Time.zone = 'Berlin'

ticket = Ticket.create!(opened_at: '2018-02-18 07:00:08 -0500')

 ticket.opened_at
 # => Sun, 18 Feb 2018 13:00:08 +0100
 ticket
 # => #<Ticket _id: 5c13d4b9026d7c4e7870bb2f, opened_at: 2018-02-18 12:00:08 UTC>

 Time.zone = 'America/New_York'
 ticket.opened_at
 # => Sun, 18 Feb 2018 07:00:08 -0500

 Mongoid.use_utc = true
 ticket.opened_at
 # => Sun, 18 Feb 2018 12:00:08 +0000

Mongoid also supports casting integers and floats to DateTime. When doing so, the integers/floats are assumed to be Unix timestamps (in UTC):

ticket.opened_at = 1544803974
ticket.opened_at
# => Fri, 14 Dec 2018 16:12:54 +0000

If a string is used as a DateTime field value, the behavior depends on whether the string includes a time zone. If no time zone is specified, the default Mongoid time zone is used:

Time.zone = 'America/New_York'
ticket.opened_at = 'Mar 4, 2018 10:00:00'
ticket.opened_at
# => Sun, 04 Mar 2018 15:00:00 +0000

If a time zone is specified, it is respected:

ticket.opened_at = 'Mar 4, 2018 10:00:00 +01:00'
ticket.opened_at
# => Sun, 04 Mar 2018 09:00:00 +0000

Regular Expression Fields

MongoDB supports storing regular expressions in documents, and querying using regular expressions. Of note for Ruby applications, MongoDB uses Perl-compatible regular expressions (PCRE) and Ruby uses Onigmo, which is a fork of Oniguruma regular expression engine. The two regular expression implementations generally provide equivalent functionality but have several important syntax differences.

When a field is declared to be of type Regexp, Mongoid converts Ruby regular expressions to BSON regular expressions and stores the result in MongoDB. Retrieving the field from the database produces a BSON::Regexp::Raw instance:

class Token
  include Mongoid::Document

  field :pattern, type: Regexp
end

token = Token.create!(pattern: /hello.world/m)
token.pattern
# => /hello.world/m

token.reload
token.pattern
# => #<BSON::Regexp::Raw:0x0000555f505e4a20 @pattern="hello.world", @options="ms">

Use #compile method on BSON::Regexp::Raw to get back the Ruby regular expression:

token.pattern.compile
# => /hello.world/m

Note that, if the regular expression was not originally a Ruby one, calling #compile on it may produce a different regular expression. For example, the following is a PCRE matching a string that ends in “hello”:

BSON::Regexp::Raw.new('hello$', 's')
# => #<BSON::Regexp::Raw:0x0000555f51441640 @pattern="hello$", @options="s">

Compiling this regular expression produces a Ruby regular expression that matches strings containing “hello” before a newline, besides strings ending in “hello”:

BSON::Regexp::Raw.new('hello$', 's').compile =~ "hello\nworld"
# => 0

This is because the meaning of $ is different between PCRE and Ruby regular expressions.

Defaults

A field can be configured to have a default value. The default value can be fixed, as in the following example:

class Order
  include Mongoid::Document

  field :state, type: String, default: 'created'
end

The default value can also be specified as a Proc:

class Order
  include Mongoid::Document

  field :fulfill_by, type: Time, default: ->{ Time.now + 3.days }
end

Note

Default values that are not Proc instances are evaluated at class load time, meaning the following two definitions are not equivalent:

field :submitted_at, type: Time, default: Time.now
field :submitted_at, type: Time, default: ->{ Time.now }

The second definition is most likely the desired one, which causes the time of submission to be set to the current time at the moment of document instantiation.

To set a default which depends on the document’s state, use self inside the Proc instance which would evaluate to the document instance being operated on:

field :fulfill_by, type: Time, default: ->{
  # Order should be fulfilled in 2 business hours.
  if (7..8).include?(self.submitted_at.hour)
    self.submitted_at + 4.hours
  elsif (9..3).include?(self.submitted_at.hour)
    self.submitted_at + 2.hours
  else
    (self.submitted_at + 1.day).change(hour: 11)
  end
}

When defining a default value as a Proc, Mongoid will apply the default after all other attributes are set. To have the default be applied before the other attributes are set, use the pre_processed: true field option:

field :fulfill_by, type: Time, default: ->{ Time.now + 3.days },
  pre_processed: true

Specifying Storage Field Names

One of the drawbacks of having a schemaless database is that MongoDB must store all field information along with every document, meaning that it takes up a lot of storage space in RAM and on disk. A common pattern to limit this is to alias fields to a small number of characters, while keeping the domain in the application expressive. Mongoid allows you to do this and reference the fields in the domain via their long names in getters, setters, and criteria while performing the conversion for you.

class Band
  include Mongoid::Document
  field :n, as: :name, type: String
end

band = Band.new(name: "Placebo")
band.attributes # { "n" => "Placebo" }

criteria = Band.where(name: "Placebo")
criteria.selector # { "n" => "Placebo" }

Field Aliases

It is possible to define field aliases. The value will be stored in the destination field but can be accessed from either the destination field or from the aliased field:

class Band
  include Mongoid::Document

  field :name, type: String
  alias_attribute :n, :name
end

band = Band.new(n: 'Astral Projection')
# => #<Band _id: 5fc1c1ee2c97a64accbeb5e1, name: "Astral Projection">

band.attributes
# => {"_id"=>BSON::ObjectId('5fc1c1ee2c97a64accbeb5e1'), "name"=>"Astral Projection"}

band.n
# => "Astral Projection"

Aliases can be removed from model classes using the unalias_attribute method.

class Band
  unalias_attribute :n
end

Unaliasing id

unalias_attribute can be used to remove the predefined id alias. This is useful for storing different values in id and _id fields:

class Band
  include Mongoid::Document

  unalias_attribute :id
  field :id, type: String
end

Band.new(id: '42')
# => #<Band _id: 5fc1c3f42c97a6590684046c, id: "42">

Custom Fields

You can define custom types in Mongoid and determine how they are serialized and deserialized. You simply need to provide three methods on it for Mongoid to call to convert your object to and from MongoDB friendly values.

class Profile
  include Mongoid::Document
  field :location, type: Point
end

class Point

  attr_reader :x, :y

  def initialize(x, y)
    @x, @y = x, y
  end

  # Converts an object of this instance into a database friendly value.
  def mongoize
    [ x, y ]
  end

  class << self

    # Get the object as it was stored in the database, and instantiate
    # this custom class from it.
    def demongoize(object)
      Point.new(object[0], object[1])
    end

    # Takes any possible object and converts it to how it would be
    # stored in the database.
    def mongoize(object)
      case object
      when Point then object.mongoize
      when Hash then Point.new(object[:x], object[:y]).mongoize
      else object
      end
    end

    # Converts the object that was supplied to a criteria and converts it
    # into a database friendly form.
    def evolve(object)
      case object
      when Point then object.mongoize
      else object
      end
    end
  end
end

The instance method mongoize takes an instance of your object, and converts it into how it will be stored in the database. In our example above, we want to store our point object as an array in the form [ x, y ].

The class method demongoize takes an object as how it was stored in the database, and is responsible for instantiating an object of your custom type. In this case, we take an array and instantiate a Point from it.

The class method mongoize takes an object that you would use to set on your model from your application code, and create the object as it would be stored in the database. This is for cases where you are not passing your model instances of your custom type in the setter:

point = Point.new(12, 24)
venue = Venue.new(location: point) # This uses the mongoize instance method.
venue = Venue.new(location: [ 12, 24 ]) # This uses the mongoize class method.

The class method evolve takes an object, and determines how it is to be transformed for use in criteria. For example we may want to write a query like so:

point = Point.new(12, 24)
Venue.where(location: point)

Note that when accessing custom fields from the document, you will get a new instance of that object with each call to the getter. This is because Mongoid is generating a new object from the raw attributes on each access.

We need the point object in the criteria to be transformed to a Mongo friendly value when it is not as well, evolve is the method that takes care of this. We check if the passed in object is a Point first, in case we also want to be able to pass in ordinary arrays instead.

Reserved Names

Attempting to define a field on a document that conflicts with a reserved method name in Mongoid will raise an error. The list of reserved names can be obtained by invoking the Mongoid.destructive_fields method.

Field Redefinition

By default Mongoid allows redefining fields on a model. To raise an error when a field is redefined, set the duplicate_fields_exception configuration option to true.

With the option set to true, the following example will raise an error:

class Person
  include Mongoid::Document

  field :name

  field :name, type: String
end

To define the field anyway, use the overwrite: true option:

class Person
  include Mongoid::Document

  field :name

  field :name, type: String, overwrite: true
end

Custom Ids

By default, Mongoid defines the _id field on documents to contain a BSON::ObjectId value which is automatically generated by Mongoid.

It is possible to replace the _id field definition to change the type of the _id values or have different default values:

class Band
  include Mongoid::Document
  field :name, type: String
  field :_id, type: String, default: ->{ name }
end

It is possible to omit the default entirely:

class Band
  include Mongoid::Document
  field :_id, type: String
end

If the default on _id is omitted, and no _id value is provided by your application, Mongoid will persist the document without the _id value. In this case, if the document is a top-level document, an _id value will be assigned by the server; if the document is an embedded document, no _id value will be assigned. Mongoid will not automatically retrieve this value, if assigned, when the document is persisted - you must obtain the persisted value (and the complete persisted document) using other means:

band = Band.create!
=> #<Band _id: , >
band.id
=> nil
band.reload
# raises Mongoid::Errors::DocumentNotFound
Band.last
=> #<Band _id: 5fc681c22c97a6791f324b99, >

Omitting _id fields is more common in embedded documents.

Mongoid also defines the id field aliased to _id. The id alias can be removed if desired (such as to integrate with systems that use the id field to store value different from _id.

Dynamic Fields

By default, Mongoid requires all fields that may be set on a document to be explicitly defined using field declarations. Mongoid also supports creating fields on the fly from an arbitrary hash or documents stored in the database. When a model uses fields not explicitly defined, such fields are called dynamic fields.

To enable dynamic fields, include Mongoid::Attributes::Dynamic module in the model:

class Person
  include Mongoid::Document
  include Mongoid::Attributes::Dynamic
end

bob = Person.new(name: 'Bob', age: 42)
bob.name
# => "Bob"

It is possible to use field declarations and dynamic fields in the same model class. Attributes for which there is a field declaration will be treated according to the field declaration, with remaining attributes being treated as dynamic fields.

Attribute values in the dynamic fields must initially be set by either passing the attribute hash to the constructor, mass assignment via attributes=, mass assignment via []=, using write_attribute, or they must already be present in the database.

# OK
bob = Person.new(name: 'Bob')

# OK
bob = Person.new
bob.attributes = {age: 42}

# OK
bob = Person.new
bob['age'] = 42

# Raises NoMethodError: undefined method age=
bob = Person.new
bob.age = 42

# OK
bob = Person.new
# OK - string access
bob.write_attribute('age', 42)
# OK - symbol access
bob.write_attribute(:name, 'Bob')

# OK, initializes attributes from whatever is in the database
bob = Person.find('123')

If an attribute is not present in a particular model instance’s attributes hash, both the reader and the writer for the corresponding field are not defined, and invoking them raises NoMethodError:

bob = Person.new
bob.attributes = {age: 42}

bob.age
# => 42

# raises NoMethodError
bob.name

# raises NoMethodError
bob.name = 'Bob'

# OK
bob['name'] = 'Bob'

bob.name
# => "Bob"

Attributes can always be read using mass attribute access or read_attribute (this applies to models not using dynamic fields as well):

bob = Person.new(age: 42)

# OK - string access
bob['name']
# => nil

# OK - symbol access
bob[:name]
# => nil

# OK - string access
bob['age']
# => 42

# OK - symbol access
bob[:age]
# => 42

# OK
bob.attributes['name']
# => nil

# OK
bob.attributes['age']
# => 42

# Returns nil - keys are always strings
bob.attributes[:age]
# => nil

# OK
bob.read_attribute('name')
# => nil

# OK
bob.read_attribute(:name)
# => nil

# OK - string access
bob.read_attribute('age')
# => 42

# OK - symbol access
bob.read_attribute(:age)
# => 42

Special Characters in Field Names

Mongoid permits dynamic field names to include spaces and punctuation:

bob = Person.new('hello world' => 'MDB')
bob.send('hello world')
# => "MDB"

bob.write_attribute("hello%world", 'MDB')
bob[:"hello%world"]
# => "MDB"

Localized Fields

Mongoid supports localized fields via i18n.

class Product
  include Mongoid::Document
  field :description, localize: true
end

By telling the field to localize, Mongoid will under the covers store the field as a hash of locale/value pairs, but normal access to it will behave like a string.

I18n.default_locale = :en
product = Product.new
product.description = "Marvelous!"
I18n.locale = :de
product.description = "Fantastisch!"

product.attributes
# { "description" => { "en" => "Marvelous!", "de" => "Fantastisch!" }

You can get and set all the translations at once by using the corresponding _translations method.

product.description_translations
# { "en" => "Marvelous!", "de" => "Fantastisch!" }
product.description_translations =
  { "en" => "Marvelous!", "de" => "Wunderbar!" }

Fallbacks

Mongoid integrates with i18n fallbacks. To use the fallbacks, the respective functionality must be explicitly enabled.

In a Rails application, set the config.i18n.fallbacks configuration setting to true in your environment and specify the fallback languages:

config.i18n.fallbacks = true
config.after_initialize do
  I18n.fallbacks[:de] = [ :en, :es ]
end

In a non-Rails application, include the fallbacks module into the I18n backend you are using and specify the fallback languages:

require "i18n/backend/fallbacks"
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
I18n.fallbacks[:de] = [ :en, :es ]

When fallbacks are enabled, if a translation is not present in the active language, translations will be looked up in the fallback languages:

product = Product.new
I18n.locale = :en
product.description = "Marvelous!"
I18n.locale = :de
product.description # "Marvelous!"

Note

In i18n 1.1, the behavior of fallbacks changed to always require an explicit list of fallback locales rather than falling back to the default locale when no fallback locales have been provided.

Querying

When querying for localized fields using Mongoid’s criteria API, Mongoid will automatically alter the criteria to match the current locale.

# Match all products with Marvelous as the description. Locale is en.
Product.where(description: "Marvelous!")
# The resulting MongoDB query filter: { "description.en" : "Marvelous!" }

Indexing

If you plan to be querying extensively on localized fields, you should index each of the locales that you plan on searching on.

class Product
  include Mongoid::Document
  field :description, localize: true

  index "description.de" => 1
  index "description.en" => 1
end

Read-Only Attributes

You can tell Mongoid that certain attributes are read-only. This will allow documents to be created with these attributes, but changes to them will be ignored when using mass update methods such as update_attributes:

class Band
  include Mongoid::Document
  field :name, type: String
  field :origin, type: String

  attr_readonly :name, :origin
end

band = Band.create(name: "Placebo")
band.update_attributes(name: "Tool") # Filters out the name change.

If you explicitly try to update or remove a read-only attribute by itself, a ReadonlyAttribute exception will be raised:

band.update_attribute(:name, "Tool") # Raises the error.
band.remove_attribute(:name) # Raises the error.

Timestamp Fields

Mongoid supplies a timestamping module in Mongoid::Timestamps which can be included to get basic behavior for created_at and updated_at fields.

class Person
  include Mongoid::Document
  include Mongoid::Timestamps
end

You may also choose to only have specific timestamps for creation or modification.

class Person
  include Mongoid::Document
  include Mongoid::Timestamps::Created
end

class Post
  include Mongoid::Document
  include Mongoid::Timestamps::Updated
end

If you want to turn off timestamping for specific calls, use the timeless method:

person.timeless.save
Person.timeless.create!

If you’d like shorter timestamp fields with aliases on them to save space, you can include the short versions of the modules.

class Band
  include Mongoid::Document
  include Mongoid::Timestamps::Short # For c_at and u_at.
end

class Band
  include Mongoid::Document
  include Mongoid::Timestamps::Created::Short # For c_at only.
end

class Band
  include Mongoid::Document
  include Mongoid::Timestamps::Updated::Short # For u_at only.
end