-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (48 loc) · 1.29 KB
/
Copy pathindex.js
File metadata and controls
69 lines (48 loc) · 1.29 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
const secret = require('./secret'); // import password
const mongoose = require('mongoose');
// Conectarse a la BD en MongoDB Atlas
mongoose.connect('mongodb+srv://'+secret.mongoUser+':'+secret.mongoPass+'@cluster0.l4iqntb.mongodb.net/?retryWrites=true&w=majority');
const User = mongoose.model('User', {
username: String,
edad: Number,
});
const crear = async () => {
const user = new User({
username: 'Gino',
edad: 39,
});
const savedUser = await user.save();
console.log(savedUser);
};
//crear();
const buscarTodos = async () => {
const users = await User.find();
console.log(users);
}
// buscarTodos();
const buscar = async () => {
const user = await User.find({ username: 'axes' });
console.log(user);
}
// buscar();
const buscarUno = async () => {
const user = await User.findOne({ username: 'axes' });
console.log(user);
}
// buscarUno();
const actualizar = async () => {
const user = await User.findOne({ username: 'axes' });
console.log(user);
user.edad = 69;
await user.save();
}
// actualizar();
const eliminar = async () => {
const user = await User.findOne({ username: 'axes' });
console.log(user);
if(user) {
// eliminar registro
await user.deleteOne();
}
}
// eliminar();