-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrupo3.js
135 lines (114 loc) · 3.2 KB
/
grupo3.js
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
e = require('mongoose')
const ObjectId = mongoose.Schema.Types.ObjectId
mongoose.connect('mongodb://localhost/mongo-intro')
// PRUEBA PARA SABER SI ME CONECTÉ A LA BASE DE DATOS
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("wiiii")
// we're connected!
})
var personaSchema = mongoose.Schema({
name: {
type: String, // String, Number, Date, Object, Array, Ref
required: true
},
// edad: Number,
direccion: {
calle: String,
numero: String,
colonia: String
},
topSeries: [String],
peso: Number,
lugar: {
type: ObjectId,
ref: 'lugar'
}
})
var lugarSchema = mongoose.Schema({
name: {
type: String,
required: true
},
coords: {
lat: Number,
lng: Number
}
})
var personaModel = mongoose.model('persona', personaSchema, 'personas')
var lugarModel = mongoose.model('lugar', lugarSchema, 'lugares')
personaModel.find({})
.populate('lugar')
.exec(function (err, personas) {
if (err) console.log(err)
console.log(personas)
// console.log(personas.map(u => u.lugares))
})
// Busca lugares y updatea las personas poniendole un lugar._id random
// var lugares = []
// lugarModel.find({}, (err, res) => {
// lugares = res
// })
// setTimeout(() => {
// personaModel.find({}, function (err, personas) {
// if (err) console.log(err)
// personas.forEach(persona => {
// let index = Math.floor(Math.random() * lugares.length)
// console.log(lugares, index)
// persona.lugar = lugares[index]._id
// persona.save(function (err, updatedPersona) {
// if (err) console.log(err)
// console.log(updatedPersona)
// })
// })
// })
// }, 500);
// QUERY A PERSONAS CON METODO find()
// let leyendoPersonas = personaModel
// .find()
// .select({name: 1, edad: 1})
// .sort({edad: 1})
// .limit(2)
// .where({name: 'Raul'})
// setTimeout(function () {
// leyendoPersonas.exec( (err, res) => {
// if (err) console.log("ERROR ")
// console.log(res[0])
// })}, 1000)
// INSERT DE PERSONA CON new personaModel y METODO save()
// let nuevaPersona = new personaModel({edad: 25, topSeries: ['Breaking Bad']})
// nuevaPersona.save((err) => {
// if (err) console.log(err)
// })
// VARIOS INSERT DE LUGARES CON EL METODO create()
// lugarModel.create({
// name: 'Chapu',
// coords: {
// lat: Math.trunc(Math.random() * 100, 4),
// lng: Math.trunc(Math.random() * 100, 4),
// }
// }, (err, lugar) => {
// if (err) console.log(err)
// console.log(lugar)
// } )
// lugarModel.create({
// name: 'Roma',
// coords: {
// lat: Math.trunc(Math.random() * 100, 4),
// lng: Math.trunc(Math.random() * 100, 4),
// }
// }, (err, lugar) => {
// if (err) console.log(err)
// console.log(lugar)
// } )
// lugarModel.create({
// name: 'Tlate',
// coords: {
// lat: Math.trunc(Math.random() * 100, 4),
// lng: Math.trunc(Math.random() * 100, 4),
// }
// }, (err, lugar) => {
// if (err) console.log(err)
// console.log(lugar)
// } )