forked from mongodb/docs-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-series.rb
36 lines (29 loc) · 974 Bytes
/
time-series.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
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'mongo'
end
Mongo::Client.new(uri) do |client|
# start-create
client = Mongo::Client.new(['<connection string>'], database: 'weather')
collection_name = 'october2024'
time_series_options = { timeField: 'timestamp' }
database = client.database
database.command(
create: collection_name,
timeseries: time_series_options
)
# end-create
# start-correct
collections = database.list_collections(filter: { name: 'october2024' }).to_a
puts collections
# end-correct
# start-insert
client = Mongo::Client.new(['<connection string>'], :database => 'your_db')
collection = client[:october2024]
document_list = [
{ temperature: 77, location: "New York City", timestamp: DateTime.new(2024, 10, 22, 6, 0, 0) },
{ temperature: 74, location: "New York City", timestamp: DateTime.new(2024, 10, 23, 6, 0, 0) }
]
collection.insert_many(document_list)
#end-insert