-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
34 lines (31 loc) · 1.01 KB
/
user.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
const mongoose = require("mongoose");
main().catch((err) => console.log(err));
async function main() {
await mongoose.connect("mongodb://localhost:27017/hardik");
const userSchema = new mongoose.Schema({
name: String,
age: Number,
// email: {
// //when you field to be required or want it to be lowercase
// type: String,
// required: true,
// lowercase: true,
// },
});
const User = mongoose.model("User", userSchema);
const user = new User({ name: "hardik", age: 28 });
const user2 = await User.create({ name: "tikul", age: 29 }); //another way of adding user with saving
// user.name = "HARDIK"; //for updating
user2.name = "Tikul";
// User.email = "hardik@gmail.com";
await user2.save(); //we have to save this time because of update.
await user.save();
console.log(user);
console.log(user2);
const filter = { name: "hardik" };
const update = { age: 30 };
let doc = await User.findOneAndUpdate(filter, update, {
new: true,
});
console.log(doc);
}