這裏講述ShemaType在每一個屬性中的默認配置狀況,就連我在沒有詳細看文檔以前,就想:
1.如何使得每一個屬性好比日期屬性,在添加document的時候自動插入日期?
2.好比我有個屬性是根據每一個幾個屬性的組合狀況而確立該屬性的值怎麼搞?
3.有一個屬性我不想添加到庫裏,在查詢的時候有但願多出這個屬性來,咋搞?
反正不少問題吧,不可能寫一大堆function來動態添加動態取消,會影響效率和查詢性能的等等諸如此類。
譬如咱們平時定義狀況是: html
var schema = new Schema({ name: String, age: Number, children: [child] });
如下是設置Schema屬性選項的關鍵字列表,解釋下常常會用到的。 正則表達式
// age屬性爲其設置數據類型和默認值 var schema = new Schema({ age: { type: Number, default: 10 }) var M = db.model('M', schema) var m = new M; console.log(m.age) // 10
如今能夠解決個人第一個疑問了
api
var _UserInfo = new Schema({ nickname: String, id: Number, name: String, gender: Boolean, birthday: {type:String, get:getBirthday},//getBirthday是一個處理日期方法 age: Number, regtime: {type:Date , default:Date.now}//當你插入文檔,自動就會生成日期 }, {versionKey:false}//這個就是處理掉自動插入文檔的__v這個屬性 );固然,在通常狀況下有一種方法來設置,也能夠有其餘方法來設置,js的靈活性沒得說,選擇性不少,請看以下代碼天然名了
// 如下屬性aNumber的類型是Number,給他設置一個默認值 4.85162342,只能對單個屬性的狀況下,說明default是關鍵字 var schema = new Schema({ aNumber: Number, default: "4.815162342" }) var M = db.model('M', schema) var m = new M; console.log(m.aNumber) // 4.815162342 // 對混合類型設置默認值狀況: var schema = new Schema({ mixed: Schema.Types.Mixed }); // schema.path('mixed')獲取類型而後使用回調函數設置默認值,爲一個空對象 schema.path('mixed').default(function () { return {}; }); // 若是你不用function回調的話,你設置的默認值會被model的實例共享 var schema = new Schema({ mixed: Schema.Types.Mixed }); schema.path('mixed').default({}); var M = db.model('M', schema); var m1 = new M; m1.mixed.added = 1; console.log(m1.mixed); // { added: 1 } var m2 = new M; console.log(m2.mixed); // { added: 1 }
// get屬性的做用能夠直觀的看出把傳入的值修改爲咱們想要的數據格式,最後按照咱們制定的格式存入文檔 function dob (val) { if (!val) return val; return (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear(); } // defining within the schema var s = new Schema({ born: { type: Date, get: dob }) // or by retreiving its SchemaType var s = new Schema({ born: Date }) s.path('born').get(dob)也能夠在查詢的時候把屬性的值轉換爲咱們想要的輸出格式,例如:
function obfuscate (cc) { return '****-****-****-' + cc.slice(cc.length-4, cc.length); } var AccountSchema = new Schema({ creditCardNumber: { type: String, get: obfuscate } }); var Account = db.model('Account', AccountSchema); //查詢並轉換指定格式輸出 Account.findById(id, function (err, found) { console.log(found.creditCardNumber); // '****-****-****-1234' });
var s = new Schema({ name: { type: String, index: true }) var s = new Schema({ loc: { type: [Number], index: 'hashed' }) var s = new Schema({ loc: { type: [Number], index: '2d', sparse: true }) var s = new Schema({ loc: { type: [Number], index: { type: '2dsphere', sparse: true }}) var s = new Schema({ date: { type: Date, index: { unique: true, expires: '1d' }}) Schema.path('my.path').index(true); Schema.path('my.date').index({ expires: 60 }); Schema.path('my.path').index({ unique: true, sparse: true });
var s = new Schema({ born: { type: Date, required: true }) // or with custom error message var s = new Schema({ born: { type: Date, required: '{PATH} is required!' }) // or through the path API Schema.path('name').required(true); // with custom error messaging Schema.path('name').required(true, 'grrr :( ');
T = db.model('T', new Schema({ x: { type: String, select: true }})); T.find(..); // field x will always be selected .. // .. unless overridden; T.find().select('-x').exec(callback);
function capitalize (val) { if ('string' != typeof val) val = ''; return val.charAt(0).toUpperCase() + val.substring(1); } // defining within the schema var s = new Schema({ name: { type: String, set: capitalize }}) // or by retreiving its SchemaType var s = new Schema({ name: String }) s.path('name').set(capitalize)
var s = new Schema({ name: { type: String, sparse: true }) Schema.path('name').index({ sparse: true });
var s = new Schema({ name: { type: String, unique: true }) Schema.path('name').index({ unique: true });
// make sure every value is equal to "something" function validator (val) { return val == 'something'; } new Schema({ name: { type: String, validate: validator }}); // with a custom error message var custom = [validator, 'Uh oh, {PATH} does not equal "something".'] new Schema({ name: { type: String, validate: custom }}); // adding many validators at a time var many = [ { validator: validator, msg: 'uh oh' } , { validator: anotherValidator, msg: 'failed' } ] new Schema({ name: { type: String, validate: many }}); // or utilizing SchemaType methods directly: var schema = new Schema({ name: 'string' }); schema.path('name').validate(validator, 'validation of `{PATH}` failed with value `{VALUE}`');