【Mongodb】---Scheme和Collections对应问题
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对应问题的更多相关文章
- MongoDB - Introduction to MongoDB, Databases and Collections
MongoDB stores BSON documents, i.e. data records, in collections; the collections in databases. Data ...
- 【Mongodb】mongoDB与mongoose---Scheme和Collections对应问题
mongodb是一个基于分布式文件存储的文档型数据库 MongoDB 是一个介于关系数据库和非关系数据库之间的产品 MongoDB 最大的特点是他支持的查询语言非常强大,而且还支持对数据建立索引 官方 ...
- mongoDB index introduction
索引为mongoDB的查询提供了有效的解决方案,如果没有索引,mongodb必须的扫描文档集中所有记录来match查询条件的记录.然而这些扫描是没有必要,而且每一次操作mongod进程会处理大量的数据 ...
- 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 ...
- 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 ...
- mongodb与mysql命令详细对比
传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象(docu ...
- Nagios监控mongodb分片集群服务实战
1,监控插件下载 Mongodb插件下载地址为:git clone git://github.com/mzupan/nagios-plugin-mongodb.git,刚開始本人这里没有安装gitpu ...
- mongodb增删改查操作
Note:mongodb存储的是文档,且文档是json格式的对象,所以增删改查都必须是json格式对象. 注:mongodb常用库和表操作,但mongodb在插入数据时,不需要先创建表. show d ...
- Python: Windows 7 64位 安装、使用 pymongo 3.2
官网tutorial: http://api.mongodb.com/python/current/tutorial.html 本教程将要告诉你如何使用pymongo模块来操作MongoDB数据库. ...
随机推荐
- Python常用网页字符串处理技巧
首先一些Python字符串处理的简易常用的用法.其他的以后用到再补充. 1.去掉重复空格 s = "hello hello hello" s = ' '.join(s.split( ...
- CSS Layout
fontline-heightcolormarginpaddingbordertext-alignbackground widthheightfloatcleardisplay 定位属性 属 性 描 ...
- ArcSDE 10.2建立SDE服务
从ArcGIS 10.1开始,arcgis官方推荐以直连方式连接SDE,因此在SDE安装时不再自动安装SDE服务,以下是手动安装SDE服务的方法 环境 服务端: oracle 11.2 64位,Arc ...
- wpa_supplicant 中的wpa_supplicant.conf
主要记录下wep加密相关的配置文件的写法. network={ ssid="static-wep-test2" key_mgmt=NONE wep_key0= //密钥索引为2, ...
- Codeforces Round #342 (Div. 2) D. Finals in arithmetic 贪心
D. Finals in arithmetic 题目连接: http://www.codeforces.com/contest/625/problem/D Description Vitya is s ...
- Yeoman+Express+Angular在Linux上开发配置方法
$mkdir ExpressWithAngularTest $cd ExpressWithAngularTest choose needed components you'd like to add ...
- 局域网两台笔记本如何使用svn
前几天我要和朋友一起开发一个网站,但是都是两台笔记本,连局域网搞的很麻烦,后来就用了git,今天突然想到要用svn,就在网上找了这个办法,结果一试便可以了,很开心 很感谢楼主,下面是我做的步骤绝对给力 ...
- SQL Server复制入门(一)----复制简介【转】
SQL Server复制入门(一)----复制简介 简介 SQL Server中的复制(Replication)是SQL Server高可用性的核心功能之一,在我看来,复制指的并不仅仅是一项技术,而是 ...
- input text框和 checkbox 连带被选中的情况
<html> <head></head> <body> <ul> <li><input type="checkb ...
- IOS网络编程——第三方类库
IOS网络编程——第三方类库 目录 概述 ASIHttpRequest AFNetworking 其他 概述 ASIHttpRequest AFNetworking 其他