这是我的 js 文件,描述了不起作用的架构和 API。当我通过命令行工具执行此操作时...架构非常简单,我已经实现了一些简单的查找命令。
'use strict'
var util = require('util');
var bcrypt = require('bcrypt');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var validatePresenceOf = function(value){
return value && value.length;
};
var toLower = function(string){
return string.toLowerCase();
};
var SportsStandings = new Schema({
'sport' : { type : String,
validate : [validatePresenceOf, 'a sport is required'],
set : toLower
},
'league' : { type : String,
validate : [validatePresenceOf, 'a league is required'],
set : toLower
},
'division' : { type : String,
validate : [validatePresenceOf, 'a division is required'],
set : toLower
},
'teamName' : { type : String,
validate : [validatePresenceOf, 'a teamName is required'],
set : toLower
},
'wins' : { type : Number, min: 0,
validate : [validatePresenceOf, 'wins is required'],
},
'losses' : { type : Number, min: 0,
validate : [validatePresenceOf, 'losses is required'],
}
});
SportsStandings.statics.findTeamRecord = function(sport, league,
division, teamName,
cb) {
return this.find({'sport' : sport, 'league' : league,
'division' : division, 'teamName': teamName}, cb);
};
SportsStandings.statics.findBySport = function(sport, cb) {
return this.find({'sport' : sport}, cb);
};
module.exports = mongoose.model('SportsStanding' , SportsStandings);
这是一个简单的节点脚本,它实例化上面导出的对象并尝试在模型上执行保存命令.....
'use strict'
var util = require('util');
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/mydb');
var SportsStanding = require('../schemas/SportsStandings');
var record = new SportsStanding({
'sport' : 'mlb',
'league' : 'AL',
'divison' : 'east',
'teamName' : 'New York Yankees',
'wins' : 10,
'losses' : 1});
record.save(function(err) {
console.log('error: ' + err);
SportsStandings.find().all(function(arr) {
console.log(arr);
console.log('length='+arr.length);
});
});
process.exit();