Mongodb通过mongoose来与数据进行操作。而mongoose是通过model来创建数据库中对应的collection

  1. mongoose.model('User', UserSchema);

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

NO!

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

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

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

  1. /*!
  2. * Produces a collection name from model `name`.
  3. *
  4. * @param {String} name a model name
  5. * @return {String} a collection name
  6. * @api private
  7. */
  8. exports.toCollectionName = function (name, options) {
  9. options = options || {};
  10. if ('system.profile' === name) return name;
  11. if ('system.indexes' === name) return name;
  12. if (options.pluralization === false) return name;
  13. return pluralize(name.toLowerCase());
  14. };
  15. /**
  16. * Pluralization rules.
  17. *
  18. * These rules are applied while processing the argument to `toCollectionName`.
  19. *
  20. * @deprecated remove in 4.x gh-1350
  21. */
  22. exports.pluralization = [
  23. [/(m)an$/gi, '$1en'],
  24. [/(pe)rson$/gi, '$1ople'],
  25. [/(child)$/gi, '$1ren'],
  26. [/^(ox)$/gi, '$1en'],
  27. [/(ax|test)is$/gi, '$1es'],
  28. [/(octop|vir)us$/gi, '$1i'],
  29. [/(alias|status)$/gi, '$1es'],
  30. [/(bu)s$/gi, '$1ses'],
  31. [/(buffal|tomat|potat)o$/gi, '$1oes'],
  32. [/([ti])um$/gi, '$1a'],
  33. [/sis$/gi, 'ses'],
  34. [/(?:([^f])fe|([lr])f)$/gi, '$1$2ves'],
  35. [/(hive)$/gi, '$1s'],
  36. [/([^aeiouy]|qu)y$/gi, '$1ies'],
  37. [/(x|ch|ss|sh)$/gi, '$1es'],
  38. [/(matr|vert|ind)ix|ex$/gi, '$1ices'],
  39. [/([m|l])ouse$/gi, '$1ice'],
  40. [/(quiz)$/gi, '$1zes'],
  41. [/s$/gi, 's'],
  42. [/([^a-z])$/, '$1'],
  43. [/$/gi, 's']
  44. ];
  45. var rules = exports.pluralization;
  46. /**
  47. * Uncountable words.
  48. *
  49. * These words are applied while processing the argument to `toCollectionName`.
  50. * @api public
  51. */
  52. exports.uncountables = [
  53. 'advice',
  54. 'energy',
  55. 'excretion',
  56. 'digestion',
  57. 'cooperation',
  58. 'health',
  59. 'justice',
  60. 'labour',
  61. 'machinery',
  62. 'equipment',
  63. 'information',
  64. 'pollution',
  65. 'sewage',
  66. 'paper',
  67. 'money',
  68. 'species',
  69. 'series',
  70. 'rain',
  71. 'rice',
  72. 'fish',
  73. 'sheep',
  74. 'moose',
  75. 'deer',
  76. 'news',
  77. 'expertise',
  78. 'status',
  79. 'media'
  80. ];
  81. var uncountables = exports.uncountables;
  82. /*!
  83. * Pluralize function.
  84. *
  85. * @author TJ Holowaychuk (extracted from _ext.js_)
  86. * @param {String} string to pluralize
  87. * @api private
  88. */
  89. function pluralize (str) {
  90. var rule, found;
  91. if (!~uncountables.indexOf(str.toLowerCase())){
  92. found = rules.filter(function(rule){
  93. return str.match(rule[0]);
  94. });
  95. if (found[0]) return str.replace(found[0][0], found[0][1]);
  96. }
  97. return str;
  98. };

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

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

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

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

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

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

  1. var mongoose = require('mongoose');
  2. var Schema = mongoose.Schema;
  3.  
  4. var BannerSchema = new Schema({
      ……
      ……
  5. }, {collection : 'banner'});
  6. module.exports = mongoose.model('Banner', BannerSchema);

【Mongodb】---Scheme和Collections对应问题的更多相关文章

  1. MongoDB - Introduction to MongoDB, Databases and Collections

    MongoDB stores BSON documents, i.e. data records, in collections; the collections in databases. Data ...

  2. 【Mongodb】mongoDB与mongoose---Scheme和Collections对应问题

    mongodb是一个基于分布式文件存储的文档型数据库 MongoDB 是一个介于关系数据库和非关系数据库之间的产品 MongoDB 最大的特点是他支持的查询语言非常强大,而且还支持对数据建立索引 官方 ...

  3. mongoDB index introduction

    索引为mongoDB的查询提供了有效的解决方案,如果没有索引,mongodb必须的扫描文档集中所有记录来match查询条件的记录.然而这些扫描是没有必要,而且每一次操作mongod进程会处理大量的数据 ...

  4. MongoDB:The Definitive Guide CHAPTER 2 Getting Started

    MongoDB is very powerful, but it is still easy to get started with. In this chapter we’ll introduce ...

  5. Using MongoDB with Web API and ASP.NET Core

    MongoDB is a NoSQL document-oriented database that allows you to define JSON based documents which a ...

  6. mongodb与mysql命令详细对比

    传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象(docu ...

  7. Nagios监控mongodb分片集群服务实战

    1,监控插件下载 Mongodb插件下载地址为:git clone git://github.com/mzupan/nagios-plugin-mongodb.git,刚開始本人这里没有安装gitpu ...

  8. mongodb增删改查操作

    Note:mongodb存储的是文档,且文档是json格式的对象,所以增删改查都必须是json格式对象. 注:mongodb常用库和表操作,但mongodb在插入数据时,不需要先创建表. show d ...

  9. Python: Windows 7 64位 安装、使用 pymongo 3.2

    官网tutorial:  http://api.mongodb.com/python/current/tutorial.html 本教程将要告诉你如何使用pymongo模块来操作MongoDB数据库. ...

随机推荐

  1. canvas 动态飞速旋转的矩形

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. 【M28】智能指针

    1.什么是智能指针? 所谓智能指针就是,看起来,用起来,感觉起来都像原始指针,但是提供了更多功能. 2.使用智能指针取代原始指针,可以获得更多的控制权.如下: a.在构造和析构的时候,可以做一些事. ...

  3. Codeforces Round #338 (Div. 2) A. Bulbs 水题

    A. Bulbs 题目连接: http://www.codeforces.com/contest/615/problem/A Description Vasya wants to turn on Ch ...

  4. Linux下高并发socket链接数测试

    一.如何增大service进程的max open files ulimit -n 只能改小max open files,不能改大.需要按照以下步骤: 修改/etc/security/limits.co ...

  5. Android定时器,推荐ScheduledThreadPoolExecutor

    Android定时器,推荐ScheduledThreadPoolExecutor 官方网址:http://developer.android.com/reference/java/util/Timer ...

  6. android IPC及原理简介

    什么是Android操作系统,所谓的Android:是基于Linux内核的软件平台和操作系统,早期由Google开发,后由开放手机联盟Open Handset Alliance)开发.   Linux ...

  7. 【JSP】JSTL使用core标签总结(不断更新中)

    使用core标签 在页面中使用taglib指令指定标签URI和prefix.如: <%@ taglib uri="http://java.sun.com/jsp/jstl/core&q ...

  8. [AngularJS+ GSAP] Greensock TimelineLite Animation Sequences

    TimelineLite is a piece of the Greensock TweenMax library that provides the ability to create sequen ...

  9. iOS xcode8提交 iOS10 “此构建版本无效” (已解决)

    近期上传应用,遇到了"此构建版本无效"的问题,如图 网查了一下,解决了这个问题:(注意:先不要急着怀疑是网络问题,重新提交,先检查问题,别问我怎么知道的...) 1:iOS10 之 ...

  10. 显式参数 VS 隐式参数

    尽量使用显示参数,而不是隐式参数,看下面实例代码. 示例1采用显示参数,示例2采用隐式参数.对于一个不熟悉MonitorManager内部构造的调用者来说,在构造MonitorManager的时候,对 ...