1

我在下面有架构定义:

var mongoose = require('mongoose');

var CategorySchema = new mongoose.Schema({
    name: {type: String, index: { unique: true }},
    description: String
});

module.exports = mongoose.model('Category', CategorySchema);

如何指定name不接受空字符串。

4

1 回答 1

5

Setting the field as required will do the trick, like follows:

var CategorySchema = new mongoose.Schema({
    name: {type: String, required: true, index: { unique: true }},
    description: String
});

Another option is to add a validating regular expression, like follows:

var CategorySchema = new mongoose.Schema({
    name: {type: String, validate: /\S+/, index: { unique: true }},
    description: String
});
于 2013-09-27T10:50:14.763 回答