-
Notifications
You must be signed in to change notification settings - Fork 12
/
mongoid_simple_example.rb
71 lines (52 loc) · 1.6 KB
/
mongoid_simple_example.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/ruby
# Copyright (c) 2016 ObjectLabs Corporation
# Distributed under the MIT license - http://opensource.org/licenses/MIT
# Written with mongoid 5.0.2
# Documentation: http://mongoid.org/en/mongoid/index.html
# A mongoid script connecting to a MongoDB database given a MongoDB Connection URI.
require 'mongoid'
Mongoid.load!('mongoid.yml', :production)
### Define Schema
class Song
include Mongoid::Document
field :decade
field :artist
field :song
field :weeksAtOne
store_in collection: 'songs'
end
### Create seed data
seventies = Song.new(
decade: '1970s',
artist: 'Debby Boone',
song: 'You Light Up My Life',
weeksAtOne: 10
)
eighties = Song.new(
decade: '1980s',
artist: 'Olivia Newton-John',
song: 'Physical',
weeksAtOne: 10
)
nineties = Song.new(
decade: '1990s',
artist: 'Mariah Carey',
song: 'One Sweet Day',
weeksAtOne: 16
)
### Write the songs to your MongoDB
seventies.save!
eighties.save!
nineties.save!
# We need to give Boyz II Men credit for their contribution to
# the hit "One Sweet Day"
Song.where(song: 'One Sweet Day').update(artist: 'Mariah Carey ft. Boyz II Men')
# Finally we run a query which returns all the hits that spent 10 or
# more weeks at number 1
songs = Song.where(:weeksAtOne.gte => 10).sort(decade: 1)
songs.each{ |doc| puts "In the #{ doc['decade'] }," +
" #{ doc['song'] } by #{ doc['artist'] }" +
" topped the charts for #{ doc['weeksAtOne'] }" +
" straight weeks." }
### Since this is an example, we"ll clean up after ourselves.
Song.collection.drop()