-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadcontacts.rb
More file actions
72 lines (60 loc) · 1.46 KB
/
loadcontacts.rb
File metadata and controls
72 lines (60 loc) · 1.46 KB
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
72
# coding: utf-8
require 'mongo'
require 'parseconfig'
# Load DB access info
config = ParseConfig.new('/Users/raul/.mongohq.conf')
# Connect to MongoHQ
db = Mongo::Connection.new(config.get_value('host'), config.get_value('port')).db('rubylearning')
auth = db.authenticate(config.get_value('user'), config.get_value('pass'))
if !auth
then
puts 'Could not authenticate to database'
exit
end
fulano = {
:firstName => 'Fulano',
:lastName => 'de Tal',
:mobile => '654321',
:email => ['fulano@example.org'] }
aitor = {
:firstName => 'Aitor',
:lastName => 'Tilla',
:homePhone => '98765',
:mobile => '67543',
:email => ['aitor@example.net']
}
dolores = {
:firstName => 'Dolores',
:lastName => 'Fuertes',
:nickname => 'Pupas',
:mobile => '68786',
:email => ['dolores@example.org', 'fuertesd@example.com']
}
pilar = {
:firstName => 'Pilar',
:lastName => 'Ica',
:homePhone => '99988',
:birthDate => Time.local(1945, 'may', 31)
}
andres = {
:firstName => "Andrés",
:lastName => 'Trozado',
:nickname => 'Trozo',
:mobile => '66554',
:email => ['andres.trozado@example.com']
}
carmelo = {
:firstName => 'Carmelo',
:lastName => "Cotón",
:email => ['coton@example.com']
}
# Select collection
coll = db.collection('contacts')
# Create an index
coll.create_index([['lastName', Mongo::ASCENDING], ['firstName', Mongo::ASCENDING]])
# Insert data
coll.insert(fulano)
coll.insert(aitor)
coll.insert(dolores)
coll.insert(pilar)
coll.insert(andres)