【Mongodb】---Scheme和Collections对应问题

来源:互联网 发布:广点通php sdk 编辑:程序博客网 时间:2024/06/07 05:23
Mongodb通过mongoose来与数据进行操作。而mongoose是通过model来创建数据库中对应的collection

mongoose.model('User', UserSchema);

 在相应的数据库中创建一个collection时,第一反应肯定会推断在对应的数据库中会建立一个‘User’的collection

NO!

大家可以尝试一下,模型名改为User、Money、Box试试看结果……

其实Mongoose在模型名至数据库集合名的命名转换上做了文章。Collection的命名做了复数和不可数处理

查看Mongoose框架的源代码,看看作者是如何做集合命名规范的, 位于mongoose/lib/util.js模块中如下代码片段是集合命名的根源。

/*! * Produces a collection name from model `name`. * * @param {String} name a model name * @return {String} a collection name * @api private */exports.toCollectionName = function (name, options) {  options = options || {};  if ('system.profile' === name) return name;  if ('system.indexes' === name) return name;  if (options.pluralization === false) return name;  return pluralize(name.toLowerCase());};/** * Pluralization rules. * * These rules are applied while processing the argument to `toCollectionName`. * * @deprecated remove in 4.x gh-1350 */exports.pluralization = [  [/(m)an$/gi, '$1en'],  [/(pe)rson$/gi, '$1ople'],  [/(child)$/gi, '$1ren'],  [/^(ox)$/gi, '$1en'],  [/(ax|test)is$/gi, '$1es'],  [/(octop|vir)us$/gi, '$1i'],  [/(alias|status)$/gi, '$1es'],  [/(bu)s$/gi, '$1ses'],  [/(buffal|tomat|potat)o$/gi, '$1oes'],  [/([ti])um$/gi, '$1a'],  [/sis$/gi, 'ses'],  [/(?:([^f])fe|([lr])f)$/gi, '$1$2ves'],  [/(hive)$/gi, '$1s'],  [/([^aeiouy]|qu)y$/gi, '$1ies'],  [/(x|ch|ss|sh)$/gi, '$1es'],  [/(matr|vert|ind)ix|ex$/gi, '$1ices'],  [/([m|l])ouse$/gi, '$1ice'],  [/(quiz)$/gi, '$1zes'],  [/s$/gi, 's'],  [/([^a-z])$/, '$1'],  [/$/gi, 's']];var rules = exports.pluralization;/** * Uncountable words. * * These words are applied while processing the argument to `toCollectionName`. * @api public */exports.uncountables = [  'advice',  'energy',  'excretion',  'digestion',  'cooperation',  'health',  'justice',  'labour',  'machinery',  'equipment',  'information',  'pollution',  'sewage',  'paper',  'money',  'species',  'series',  'rain',  'rice',  'fish',  'sheep',  'moose',  'deer',  'news',  'expertise',  'status',  'media'];var uncountables = exports.uncountables;/*! * Pluralize function. * * @author TJ Holowaychuk (extracted from _ext.js_) * @param {String} string to pluralize * @api private */function pluralize (str) {    var rule, found;    if (!~uncountables.indexOf(str.toLowerCase())){      found = rules.filter(function(rule){        return str.match(rule[0]);      });      if (found[0]) return str.replace(found[0][0], found[0][1]);    }  return str;};

上面代码 对集合名称做了处理,uncountables是不可数名词,rules是一组正则匹配规则。   

 function pluralize(str)方法的处理思路是:

      1.判断模型名是否是不可数的,如果是直接返回模型名;否则进行复数转化正则匹配;

      2.返回复数转化正则匹配结果(一个复数转化正则匹配是一个数组,有两个对象,[0]正则表达式,[1]匹配后处理结果);

      3.如果复数转化正则匹配结果不存在,直接返回模型名;否则取匹配结果第一个,对模型名进行处理。(需要说明的是,rules是按特殊到一般的顺序排列的)

 如果想模型对应制定的collection,可以这样解决

var mongoose = require('mongoose');var Schema = mongoose.Schema;var BannerSchema = new Schema({
  ……
  ……}, {collection : 'banner'});module.exports = mongoose.model('Banner', BannerSchema);
0 0
原创粉丝点击