博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Mongodb】---Scheme和Collections对应问题
阅读量:4708 次
发布时间:2019-06-10

本文共 2956 字,大约阅读时间需要 9 分钟。

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);

转载于:https://www.cnblogs.com/itguliang/p/4497264.html

你可能感兴趣的文章
接口测试的两种方法(转自 http://www.blogjava.net/qileilove/archive/2012/05/31/379631.html )...
查看>>
20172328《程序设计与数据结构》第七周学习总结
查看>>
Android中内容观察者的使用---- ContentObserver类详解
查看>>
统计图表--第三方开源--MPAndroidChart(一)
查看>>
Leetcode: Multiply Strings
查看>>
java获取登录用户ip地址
查看>>
Tomcat安装与使用
查看>>
Java课程总结
查看>>
android如何用adb shell启动应用程序
查看>>
网站安全
查看>>
实战深度学习OpenCV(一):canny边缘检测
查看>>
责任链模式(chain of responsibility)
查看>>
[转载]java多线程学习-java.util.concurrent详解(一) Latch/Barrier
查看>>
ionic - 运行起来
查看>>
Shell 输入/输出重定向
查看>>
数据结构与算法分析(C++)读书笔记
查看>>
(转)nginx应用总结(1)--基础认识和应用参数优化配置
查看>>
(转)关于sql和MySQL的语句执行顺序(必看!!!)
查看>>
UVALive 3668 A Funny Stone Game(博弈)
查看>>
信息论随笔2: 交叉熵、相对熵
查看>>