web:javascript:mean_stack:mongoose
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
web:javascript:mean_stack:mongoose [2014/12/28 22:38] – sgariepy | web:javascript:mean_stack:mongoose [2022/02/02 00:42] (Version actuelle) – modification externe 127.0.0.1 | ||
---|---|---|---|
Ligne 35: | Ligne 35: | ||
isActive: | isActive: | ||
}); | }); | ||
+ | </ | ||
+ | |||
+ | ==== Valeurs par défaut ==== | ||
+ | |||
+ | <code javascript> | ||
+ | created: { | ||
+ | type: Date, | ||
+ | default: Date.now | ||
+ | } | ||
</ | </ | ||
Ligne 368: | Ligne 377: | ||
}); | }); | ||
</ | </ | ||
+ | |||
+ | |||
+ | ==== findByIdAndUpdate ==== | ||
+ | |||
+ | |||
+ | ^ Option | ||
+ | | new | Set to true to return the modified document rather than the original. | ||
+ | | upsert | ||
+ | | select | ||
+ | |||
+ | |||
+ | |||
===== Suppression ===== | ===== Suppression ===== | ||
Ligne 402: | Ligne 423: | ||
- | + | ==== findByIdAndRemove | |
- | ===== findByIdAndUpdate ===== | + | |
^ Option | ^ Option | ||
- | | new | Set to true to return the modified document rather than the original. | ||
- | | upsert | ||
| select | | select | ||
- | ===== findByIdAndRemove ===== | ||
- | ^ Option | ||
- | | select | ||
+ | ===== Autres exemples de requêtes ===== | ||
+ | <code javascript> | ||
+ | var query = Standup.find(); | ||
+ | query.sort({ createdOn: ' | ||
+ | | ||
+ | | ||
+ | // code callback | ||
+ | }); | ||
+ | </ | ||
====== Validation ====== | ====== Validation ====== | ||
+ | |||
+ | |||
+ | ^ Schema Type ^ Built-in Validators | ||
+ | | String | ||
+ | | Number | ||
+ | | Date | required | ||
+ | | Buffer | ||
+ | | Boolean | ||
+ | | Mixed | required | | | | ||
+ | | ObjectId | ||
+ | | Array | required | | | | ||
+ | |||
+ | ===== Validation required ===== | ||
+ | |||
+ | |||
+ | Exemple de schema : | ||
+ | |||
+ | <code javascript> | ||
+ | var customerSchema = new Schema({ | ||
+ | name: { type: String, required: true }, address: String, | ||
+ | city: String, | ||
+ | state: String, | ||
+ | country: String, | ||
+ | zipCode: Number, | ||
+ | createdOn: Date, | ||
+ | isActive: Boolean | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | |||
+ | Une fois le schema définit, on peut préciser une validation avec '' | ||
+ | required(required, | ||
+ | |||
+ | |||
+ | <code javascript> | ||
+ | customerSchema.path(' | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Validation match ===== | ||
+ | |||
+ | <code javascript> | ||
+ | var reMatch = /[a-zA-Z]/; | ||
+ | var customerSchema = new Schema({ | ||
+ | name: { type: String, | ||
+ | required: true, | ||
+ | match: reMatch }, | ||
+ | // etc... | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Validation enum ===== | ||
+ | |||
+ | <code javascript> | ||
+ | var impediments = [' | ||
+ | |||
+ | var standupSchema = new Schema({ | ||
+ | // abbreviated... | ||
+ | impediment: { | ||
+ | type: String, | ||
+ | required: true, | ||
+ | enum: impediments | ||
+ | } | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Validation min et max ===== | ||
+ | |||
+ | |||
+ | <code javascript> | ||
+ | var customerSchema = new Schema({ | ||
+ | name: String, | ||
+ | discount: { type: Number, min: 5 } | ||
+ | }); | ||
+ | |||
+ | var customerSchema = new Schema({ | ||
+ | name: String, | ||
+ | discount: { type: Number, max: 60 } | ||
+ | }); | ||
+ | |||
+ | var customerSchema = new Schema({ | ||
+ | name: String, | ||
+ | discount: { type: Number, min: 5, max: 60 } | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | ===== Validation personnalisée ===== | ||
+ | |||
+ | Une validation personnalisée peut être spécifiée avec '' | ||
+ | |||
+ | <code javascript> | ||
+ | var sizeValidator = [ | ||
+ | function (val) { | ||
+ | return (val.length > 0 && val.length <= 50) | ||
+ | }, | ||
+ | // Custom error text... | ||
+ | ' | ||
+ | ]; | ||
+ | |||
+ | var personSchema = new Schema({ | ||
+ | firstName: { type: String, required: true, validate: sizeValidator }, | ||
+ | lastName: { type: String, required: true, validate: sizeValidator }, | ||
+ | status: { type: String, required: true, default: ' | ||
+ | }); | ||
+ | |||
+ | var Person = new mongoose.model(' | ||
+ | |||
+ | var newPerson = new Person( { firstName: ' | ||
+ | |||
+ | newPerson.save(function (err) { | ||
+ | if (err) return handleError(err); | ||
+ | // saved the person document! | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
web/javascript/mean_stack/mongoose.1419802720.txt.gz · Dernière modification : 2022/02/02 00:43 (modification externe)