-
Notifications
You must be signed in to change notification settings - Fork 5
Mongoose
gyim1345 edited this page Dec 21, 2020
·
2 revisions
Mongooseλ λ°μ΄ν°λ₯Ό μν λͺ¨λΈλ§ νκ²½μ μ 곡νλ ODM(object data modeling) λΌμ΄λΈλ¬λ¦¬λ€.
// user schema μ μ
const userSchema = new mongoose.Schema({
name: {
first: String,
last: { type: String, trim: true }
},
age: { type: Number, min: 0 }
});
// user schema μ¬μ©νμ¬ λͺ¨λΈ μμ±
const PUser = mongoose.model('PowerUsers', userSchema);
// λͺ¨λΈλ‘ μ μ μμ±
const johndoe = new PUser ({
name: { first: 'John', last: ' Doe ' },
age: 25
});
// μ μ₯
johndoe.save(function (err) {if (err) console.log ('Error on save!')});
λ°μ΄ν°μ type validationκ³Ό κ·ΈμΈ μμνκ²μ μ½κ² λμμ€μ λ무 μ’μλ€. μ€μ λ λ무 μ½κ² λμ μ’μλ€. κ°λ Ή defaultκ°μ μΆκ° νΉμ μμ ν λ.