Skip to content

Commit b705149

Browse files
authored
Merge pull request #61 from rustagir/DOCSP-44821-specify-query-2
DOCSP-44821: specify a query pt 2
2 parents 246bcc5 + ca219dc commit b705149

File tree

4 files changed

+611
-5
lines changed

4 files changed

+611
-5
lines changed

source/includes/interact-data/query.rb

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,182 @@
160160
# {"year"=>{"$in"=>[1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960]}}
161161
# end-range-query
162162

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

169+
swans = Band.create!(name: 'Swans', tours: [
170+
{city: 'Milan', year: 2014},
171+
{city: 'Montreal', year: 2015},
172+
])
173+
174+
# Returns only "Aerosmith"
175+
Band.elem_match(tours: {city: 'London'})
176+
# end-elem-match-1
177+
178+
# start-elemmatch-embedded-class
179+
class Band
180+
include Mongoid::Document
181+
field :name, type: String
182+
embeds_many :tours
183+
end
184+
185+
class Tour
186+
include Mongoid::Document
187+
field :city, type: String
188+
field :year, type: Integer
189+
embedded_in :band
190+
end
191+
# end-elemmatch-embedded-class
192+
193+
# start-elemmatch-embedded-operations
194+
aerosmith = Band.create!(name: 'Aerosmith')
195+
196+
Tour.create!(band: aerosmith, city: 'London', year: 1995)
197+
Tour.create!(band: aerosmith, city: 'New York', year: 1999)
198+
199+
# Returns the "Aerosmith" document
200+
Band.elem_match(tours: {city: 'London'})
201+
# end-elemmatch-embedded-operations
202+
203+
# start-elemmatch-recursive
204+
class Tag
205+
include Mongoid::Document
206+
207+
field name:, type: String
208+
recursively_embeds_many
209+
end
210+
211+
# Creates the root Tag
212+
root = Tag.create!(name: 'root')
213+
214+
# Adds embedded Tags
215+
sub1 = Tag.new(name: 'sub_tag_1', child_tags: [Tag.new(name: 'sub_sub_tag_1')])
216+
217+
root.child_tags << sub1
218+
root.child_tags << Tag.new(name: 'sub_tag_2')
219+
root.save!
220+
221+
# Searches for Tag in which one child Tag tame is "sub_tag_1"
222+
Tag.elem_match(child_tags: {name: 'sub_tag_1'})
223+
224+
# Searches for a child Tag in which one child Tag tame is "sub_sub_tag_1"
225+
root.child_tags.elem_match(child_tags: {name: 'sub_sub_tag_1'})
226+
# end-elemmatch-recursive
227+
228+
# start-id-query-multiple
229+
# Equivalent ways to match multiple documents
230+
Band.find('5f0e41d92c97a64a26aabd10', '5f0e41b02c97a64a26aabd0e')
231+
Band.find(['5f0e41d92c97a64a26aabd10', '5f0e41b02c97a64a26aabd0e'])
232+
# end-id-query-multiple
233+
234+
# start-ordinal-examples
235+
# Returns the first document in the collection
236+
Band.first
237+
238+
# Returns the first matching document
239+
Band.where(founded: {'$gt' => 1980}).first
240+
241+
# Returns the first two matching documents
242+
Band.first(2)
243+
244+
# Returns the last matching document
245+
Band.where(founded: {'$gt' => 1980}).last
246+
247+
# Returns the second to last document
248+
Band.second_to_last
249+
# end-ordinal-examples
250+
251+
# start-field-val-examples
252+
Band.distinct(:name)
253+
# Example output: "Ghost Mountain" "Hello Goodbye" "She Said"
254+
255+
Band.where(:members.gt => 2).distinct(:name)
256+
# Example output: "Arctic Monkeys" "The Smiths"
257+
258+
Band.distinct('tours.city')
259+
# Example output: "London" "Sydney" "Amsterdam"
260+
261+
Band.all.pick(:name)
262+
# Example output: "The Smiths"
263+
264+
Band.all.pluck(:country)
265+
# Example output: "England" "Spain" "England" "Japan"
266+
267+
Band.all.tally(:country)
268+
# Example output: ["England",2] ["Italy",3]
269+
# end-field-val-examples
270+
271+
# start-query-findby
272+
# Simple equality query
273+
Band.find_by(name: "Photek")
274+
275+
# Performs an action on each returned result
276+
Band.find_by(name: "Tool") do |band|
277+
band.fans += 1
278+
end
279+
# end-query-findby
280+
281+
# start-query-find-or-create
282+
# If no matches, creates a Band with just the "name" field
283+
Band.find_or_create_by(name: "Photek")
284+
285+
# If no matches, creates a Band with just the "name" field because the
286+
# query condition is not a literal
287+
Band.where(:likes.gt => 10).find_or_create_by(name: "Photek")
288+
289+
# Creates a Band in which the name is Aerosmith because there is no
290+
# document in which "name" is Photek and Aerosmith at the same time
291+
Band.where(name: "Photek").find_or_create_by(name: "Aerosmith")
292+
# end-query-find-or-create
293+
294+
# start-regex
295+
# Matches "description" values that start exactly with "Impala"
296+
Band.where(description: /\AImpala/)
297+
# => nil
298+
299+
# Matches "description" values that start exactly with "Impala"
300+
Band.where(description: BSON::Regexp::Raw.new('^Impala'))
301+
# => nil
302+
303+
# Matches "description" values that start exactly with "Impala" with
304+
# the multiline option
305+
Band.where(description: BSON::Regexp::Raw.new('^Impala', 'm'))
306+
# => Returns sample document
307+
# end-regex
308+
309+
# start-field-conversion-model
310+
class Album
311+
include Mongoid::Document
312+
313+
field :release_date, type: Date
314+
field :last_commented, type: Time
315+
field :last_purchased
316+
end
317+
# end-field-conversion-model
318+
319+
# start-date-queries-1
320+
Album.where(release_date: Date.today)
321+
# Interpreted query:
322+
# {"release_date"=>2024-11-05 00:00:00 UTC}
323+
324+
Album.where(last_commented: Time.now)
325+
# Interpreted query:
326+
# {"last_commented"=>2024-11-04 17:20:47.329472 UTC}
327+
# end-date-queries-1
328+
329+
# start-date-queries-2
330+
Album.where(last_commented: Date.today)
331+
# Interpreted query:
332+
# {"last_commented"=>Mon, 04 Nov 2024 00:00:00.000000000 EST -05:00}
333+
334+
Album.where(last_purchased: Date.today)
335+
# Interpreted query:
336+
# {"last_purchased"=>"2024-11-04"}
337+
338+
Album.where(last_reviewed: Date.today)
339+
# Interpreted query:
340+
# {"last_reviewed"=>2024-11-04 00:00:00 UTC}
341+
# end-date-queries-2

source/interact-data/modify-results.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ The following code retrieves a maximum of ``5`` documents:
252252
:language: ruby
253253
:dedent:
254254

255+
.. note::
256+
257+
Alternatively, you can use the ``take()`` method to retrieve a
258+
specified number of documents from the database:
259+
260+
.. code-block:: ruby
261+
262+
Band.take(5)
263+
255264
Skip Results
256265
~~~~~~~~~~~~
257266

0 commit comments

Comments
 (0)