-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate_db.js
More file actions
157 lines (121 loc) · 2.52 KB
/
populate_db.js
File metadata and controls
157 lines (121 loc) · 2.52 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const mongoose = require('mongoose')
const connexion = 'mongodb://localhost:27017,localhost:27018,localhost:27019/example'
mongoose.connect(`${connexion}`, { replicaSet: 'rs' })
const Book = require('./models/book')
const Author = require('./models/author')
const City = require('./models/city')
const Category = require('./models/category')
mongoose.connection.once('open', () => {
console.log('conneted to database')
})
function handleError (err) {
console.log('HUBO UN ERROR:', JSON.stringify(err))
}
const carlos_paz = new City({
name: "carlos paz",
population: 50000
})
const plottier = new City({
name: "plottier",
population: 40000
})
const rosario = new City({
name: "rosario",
population: 1000000
})
const bariloche = new City({
name: "bariloche",
population: 100000
})
carlos_paz.save(function (err) {
if (err) return handleError(err);
})
plottier.save(function (err) {
if (err) return handleError(err);
})
rosario.save(function (err) {
if (err) return handleError(err);
})
bariloche.save(function (err) {
if (err) return handleError(err);
})
const junior = new Author({
name: "junior",
age: 25,
cityID: plottier._id
})
junior.save(function (err) {
if (err) return handleError(err);
});
const claudio = new Author({
name: "claudio",
age: 30,
cityID: carlos_paz._id
})
claudio.save(function (err) {
if (err) return handleError(err);
});
const dario = new Author({
name: "dario",
age: 42,
cityID: rosario._id
})
dario.save(function (err) {
if (err) return handleError(err);
});
const paola = new Author({
name: "paola",
age: 28,
cityID: bariloche._id
})
paola.save(function (err) {
if (err) return handleError(err);
});
console.log(junior._id);
const cat1 = new Category({
name: "economia"
});
cat1.save();
const cat2 = new Category({
name: "ciencia"
});
cat2.save()
const book1 = new Book({
_id: new mongoose.Types.ObjectId(),
pages: 100,
name: "book junior 1",
authorID: junior._id,
ISBN: {
country: "argentina",
number: 111
},
categories:[
{
category_ID: cat2._id
},
{
category_ID: cat1._id
},
]
});
book1.save(function (err) {
if (err) return handleError(err);
});
const book2 = new Book({
_id: new mongoose.Types.ObjectId(),
pages: 200,
name: "book claudio 1",
authorID: claudio._id,
ISBN: {
country: "argentina",
number: 222
},
categories:[
{
category_ID: cat1._id
}
]
});
book2.save(function (err) {
if (err) return handleError(err);
});