Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions source/includes/interact-data/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,182 @@
# {"year"=>{"$in"=>[1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960]}}
# end-range-query

# start-elem-match-1
aerosmith = Band.create!(name: 'Aerosmith', tours: [
{city: 'London', year: 1995},
{city: 'New York', year: 1999},
])

swans = Band.create!(name: 'Swans', tours: [
{city: 'Milan', year: 2014},
{city: 'Montreal', year: 2015},
])

# Returns only "Aerosmith"
Band.elem_match(tours: {city: 'London'})
# end-elem-match-1

# start-elemmatch-embedded-class
class Band
include Mongoid::Document
field :name, type: String
embeds_many :tours
end

class Tour
include Mongoid::Document
field :city, type: String
field :year, type: Integer
embedded_in :band
end
# end-elemmatch-embedded-class

# start-elemmatch-embedded-operations
aerosmith = Band.create!(name: 'Aerosmith')

Tour.create!(band: aerosmith, city: 'London', year: 1995)
Tour.create!(band: aerosmith, city: 'New York', year: 1999)

# Returns the "Aerosmith" document
Band.elem_match(tours: {city: 'London'})
# end-elemmatch-embedded-operations

# start-elemmatch-recursive
class Tag
include Mongoid::Document

field name:, type: String
recursively_embeds_many
end

# Creates the root Tag
root = Tag.create!(name: 'root')

# Adds embedded Tags
sub1 = Tag.new(name: 'sub_tag_1', child_tags: [Tag.new(name: 'sub_sub_tag_1')])

root.child_tags << sub1
root.child_tags << Tag.new(name: 'sub_tag_2')
root.save!

# Searches for Tag in which one child Tag tame is "sub_tag_1"
Tag.elem_match(child_tags: {name: 'sub_tag_1'})

# Searches for a child Tag in which one child Tag tame is "sub_sub_tag_1"
root.child_tags.elem_match(child_tags: {name: 'sub_sub_tag_1'})
# end-elemmatch-recursive

# start-id-query-multiple
# Equivalent ways to match multiple documents
Band.find('5f0e41d92c97a64a26aabd10', '5f0e41b02c97a64a26aabd0e')
Band.find(['5f0e41d92c97a64a26aabd10', '5f0e41b02c97a64a26aabd0e'])
# end-id-query-multiple

# start-ordinal-examples
# Returns the first document in the collection
Band.first

# Returns the first matching document
Band.where(founded: {'$gt' => 1980}).first

# Returns the first two matching documents
Band.first(2)

# Returns the last matching document
Band.where(founded: {'$gt' => 1980}).last

# Returns the second to last document
Band.second_to_last
# end-ordinal-examples

# start-field-val-examples
Band.distinct(:name)
# Example output: "Ghost Mountain" "Hello Goodbye" "She Said"

Band.where(:members.gt => 2).distinct(:name)
# Example output: "Arctic Monkeys" "The Smiths"

Band.distinct('tours.city')
# Example output: "London" "Sydney" "Amsterdam"

Band.all.pick(:name)
# Example output: "The Smiths"

Band.all.pluck(:country)
# Example output: "England" "Spain" "England" "Japan"

Band.all.tally(:country)
# Example output: ["England",2] ["Italy",3]
# end-field-val-examples

# start-query-findby
# Simple equality query
Band.find_by(name: "Photek")

# Performs an action on each returned result
Band.find_by(name: "Tool") do |band|
band.fans += 1
end
# end-query-findby

# start-query-find-or-create
# If no matches, creates a Band with just the "name" field
Band.find_or_create_by(name: "Photek")

# If no matches, creates a Band with just the "name" field because the
# query condition is not a literal
Band.where(:likes.gt => 10).find_or_create_by(name: "Photek")

# Creates a Band in which the name is Aerosmith because there is no
# document in which "name" is Photek and Aerosmith at the same time
Band.where(name: "Photek").find_or_create_by(name: "Aerosmith")
# end-query-find-or-create

# start-regex
# Matches "description" values that start exactly with "Impala"
Band.where(description: /\AImpala/)
# => nil

# Matches "description" values that start exactly with "Impala"
Band.where(description: BSON::Regexp::Raw.new('^Impala'))
# => nil

# Matches "description" values that start exactly with "Impala" with
# the multiline option
Band.where(description: BSON::Regexp::Raw.new('^Impala', 'm'))
# => Returns sample document
# end-regex

# start-field-conversion-model
class Album
include Mongoid::Document

field :release_date, type: Date
field :last_commented, type: Time
field :last_purchased
end
# end-field-conversion-model

# start-date-queries-1
Album.where(release_date: Date.today)
# Interpreted query:
# {"release_date"=>2024-11-05 00:00:00 UTC}

Album.where(last_commented: Time.now)
# Interpreted query:
# {"last_commented"=>2024-11-04 17:20:47.329472 UTC}
# end-date-queries-1

# start-date-queries-2
Album.where(last_commented: Date.today)
# Interpreted query:
# {"last_commented"=>Mon, 04 Nov 2024 00:00:00.000000000 EST -05:00}

Album.where(last_purchased: Date.today)
# Interpreted query:
# {"last_purchased"=>"2024-11-04"}

Album.where(last_reviewed: Date.today)
# Interpreted query:
# {"last_reviewed"=>2024-11-04 00:00:00 UTC}
# end-date-queries-2
9 changes: 9 additions & 0 deletions source/interact-data/modify-results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ The following code retrieves a maximum of ``5`` documents:
:language: ruby
:dedent:

.. note::

Alternatively, you can use the ``take()`` method to retrieve a
specified number of documents from the database:

.. code-block:: ruby

Band.take(5)

Skip Results
~~~~~~~~~~~~

Expand Down
Loading
Loading