var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test01'); var PersonSchema = new mongoose.Schema({ name:{ first:String, last:String } }); PersonSchema.virtual('name.full').get(function(){ return this.name.first + ' ' + this.name.last; }); PersonSchema.virtual('name.full').set(function(name){ var split = name.split(' '); this.name.first = split[0]; this.name.last = split[1]; }); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function callback () { var PersonModel = mongoose.model('Person',PersonSchema); var person1 = new PersonModel({ name:{first:'koma',last:'endia'} }); console.log("person1=" + person1.name.first + " " + person1.name.last); console.log("person1=" + person1.name.full); var person2 = new PersonModel({}); person2.name.full = "koma endia"; console.log("person2=" + person2.name.first + " " + person2.name.last); console.log("person2=" + person2.name.full); });
$node virtual.js
person1=koma endia
person1=koma endia
person2=koma endia
person2=koma endia
0 件のコメント:
コメントを投稿