// define a schemavar animalSchema =newSchema({ name: String, type: String });// assign a function to the "methods" object of our animalSchemaanimalSchema.methods.findSimilarTypes=function(cb) {returnthis.model('Animal').find({ type:this.type }, cb);};
现在我们所有的animal的实例有一个findsimilartypes方法可用。
var Animal =mongoose.model('Animal', animalSchema);var dog =newAnimal({ type:'dog' });dog.findSimilarTypes(function(err, dogs) {console.log(dogs); // woof});
// assign a function to the "statics" object of our animalSchemaanimalSchema.statics.findByName=function(name, cb) {returnthis.find({ name:newRegExp(name,'i') }, cb);};var Animal =mongoose.model('Animal', animalSchema);Animal.findByName('fido',function(err, animals) {console.log(animals);});
var schema =newSchema({ name: String, inventory: {} });var Character =mongoose.model('Character', schema);// will store `inventory` field if it is not emptyvar frodo =newCharacter({ name:'Frodo', inventory: { ringOfPower:1 }});Character.findOne({ name:'Frodo' },function(err, character) {console.log(character); // { name: 'Frodo', inventory: { ringOfPower: 1 }}});// will not store `inventory` field if it is emptyvar sam =newCharacter({ name:'Sam', inventory: {}});Character.findOne({ name:'Sam' },function(err, character) {console.log(character); // { name: 'Sam' }});
这种行为可以通过设置minimize选项为false。它将存储空的对象。
var schema =newSchema({ name: String, inventory: {} }, { minimize:false });var Character =mongoose.model('Character', schema);// will store `inventory` if emptyvar sam =newCharacter({ name:'Sam', inventory: {}});Character.findOne({ name:'Sam' },function(err, character) {console.log(character); // { name: 'Sam', inventory: {}}});
var thingSchema =newSchema({..})var Thing =mongoose.model('Thing', thingSchema);var thing =newThing({ iAmNotInTheSchema:true });thing.save(); // iAmNotInTheSchema is not saved to the db// set to false..var thingSchema =newSchema({..}, { strict:false });var thing =newThing({ iAmNotInTheSchema:true });thing.save(); // iAmNotInTheSchema is now saved to the db!!
使用doc.set()来设置属性值这样也会起作用。
var thingSchema =newSchema({..})var Thing =mongoose.model('Thing', thingSchema);var thing =new Thing;thing.set('iAmNotInTheSchema',true);thing.save(); // iAmNotInTheSchema is not saved to the db
var thingSchema =newSchema({..})var Thing =mongoose.model('Thing', thingSchema);var thing =new Thing;thing.iAmNotInTheSchema =true;thing.save(); // iAmNotInTheSchema is never saved to the db
选项: toJSON
Exactly the same as the toObject option but only applies when the documents toJSON method is called.
完全一样的toObject选项,但只适用于当文件tojson方法称为。
var schema =newSchema({ name: String });schema.path('name').get(function (v) {return v +' is my name';});schema.set('toJSON', { getters:true, virtuals:false });varM=mongoose.model('Person', schema);var m =newM({ name:'Max Headroom' });console.log(m.toObject()); // { _id: 504e0cd7dd992d9be2f20b6f, name: 'Max Headroom' }console.log(m.toJSON()); // { _id: 504e0cd7dd992d9be2f20b6f, name: 'Max Headroom is my name' }// since we know toJSON is called whenever a js object is stringified:console.log(JSON.stringify(m)); // { "_id": "504e0cd7dd992d9be2f20b6f", "name": "Max Headroom is my name" }
var schema =newSchema({ name: String });schema.path('name').get(function (v) {return v +' is my name';});schema.set('toObject', { getters:true });varM=mongoose.model('Person', schema);var m =newM({ name:'Max Headroom' });console.log(m); // { _id: 504e0cd7dd992d9be2f20b6f, name: 'Max Headroom is my name' }
var schema =newSchema({// Mongoose interpets this as 'loc is an object with 2 keys, type and coordinates' loc: { type: String, coordinates: [Number] },// Mongoose interprets this as 'name is a String' name: { $type: String }}, { typeKey:'$type' }); // A '$type' key means this object is a type declaration
var schema =newSchema({ name: String });schema.set('validateBeforeSave',false);schema.path('name').validate(function (value) {return v !=null;});varM=mongoose.model('Person', schema);var m =newM({ name:null });m.validate(function(err) {console.log(err); // Will tell you that null is not allowed.});m.save(); // Succeeds despite being invalid