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

【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. <meta http-equiv = "X-UA-Compatible" cotent = "IE=edge,chrome=1"/>

    <meta http-equiv = "X-UA-Compatible" cotent = "IE=edge,chrome=1"/> 制定ie调用哪 ...

  2. Xcode快捷键整理

    下面是Xcode比较常用的快捷键,特别是红色标注的,很常用.用熟了开发编辑代码的的时候就很方便,希望对大家有用~1. 文件CMD + N: 新文件CMD + SHIFT + N: 新项目CMD + O ...

  3. List排序忽略大小写

    public List<String> sortListIgnoreCase(List<String> list) {        Collections.sort(list ...

  4. hotfix分析

    使用System Update Readiness Tool for Windows Server 2008 R2 x64 可以分析hotfix是否有安装失败的情况 示例:http://blogs.t ...

  5. 从Windows 服务器通过sync向Linux服务器定时同步文件

    本文解决的是Windows 下目录及文件向Linux同步的问题,Windows向 Windows同步的请参考:http://www.idcfree.com/article-852-1.html 环境介 ...

  6. .NET连接SAP系统专题:C#调用RFC代码(三)

    本文就说明在C#中如何编写代码来调用SAP中的RFC函数获取数据. 首先需要引用两个NCO3.0的DLL DLL下载地址:http://files.cnblogs.com/mengxin523/SAP ...

  7. Codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers 高精度比大小,模拟

    A. Comparing Two Long Integers 题目连接: http://www.codeforces.com/contest/616/problem/A Description You ...

  8. hdu 5277 YJC counts stars 暴力

    YJC counts stars Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  9. linux C 执行多个文件

  10. 在编辑模式中一个ASP.NET应用详细视图显示集合属性 编辑模式和只读模式

    https://documentation.devexpress.com/#Xaf/CustomDocument3230