Read

Back in Chapter 2, Schema Design and Data Modelingwe described how to install, connect, and set up models, including inheritance to Mongoid. Here we will go through the most common use cases of CRUD.

Finding documents is done using a DSL similar to Active Record:

Book.find('592149c4aabac953a3a1e31e')

This will find by ObjectId and return the document with isbn 101, as will the query by name attribute:

Book.where(name: 'Mastering MongoDB')

In a similar fashion to dynamically generated active record queries by attribute, we can use the helper:

Book.find_by(name: 'Mastering MongoDB')

This queries by attribute name, equivalent to the previous query.

We should enable QueryCache to avoid hitting the database for the same query multiple times, like this:

Mongoid::QueryCache.enabled = true

This can be added in any code block we want to enable or in the initializer for Mongoid.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.35.255