【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数据库. ...
随机推荐
- canvas 动态飞速旋转的矩形
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 【M28】智能指针
1.什么是智能指针? 所谓智能指针就是,看起来,用起来,感觉起来都像原始指针,但是提供了更多功能. 2.使用智能指针取代原始指针,可以获得更多的控制权.如下: a.在构造和析构的时候,可以做一些事. ...
- Codeforces Round #338 (Div. 2) A. Bulbs 水题
A. Bulbs 题目连接: http://www.codeforces.com/contest/615/problem/A Description Vasya wants to turn on Ch ...
- Linux下高并发socket链接数测试
一.如何增大service进程的max open files ulimit -n 只能改小max open files,不能改大.需要按照以下步骤: 修改/etc/security/limits.co ...
- Android定时器,推荐ScheduledThreadPoolExecutor
Android定时器,推荐ScheduledThreadPoolExecutor 官方网址:http://developer.android.com/reference/java/util/Timer ...
- android IPC及原理简介
什么是Android操作系统,所谓的Android:是基于Linux内核的软件平台和操作系统,早期由Google开发,后由开放手机联盟Open Handset Alliance)开发. Linux ...
- 【JSP】JSTL使用core标签总结(不断更新中)
使用core标签 在页面中使用taglib指令指定标签URI和prefix.如: <%@ taglib uri="http://java.sun.com/jsp/jstl/core&q ...
- [AngularJS+ GSAP] Greensock TimelineLite Animation Sequences
TimelineLite is a piece of the Greensock TweenMax library that provides the ability to create sequen ...
- iOS xcode8提交 iOS10 “此构建版本无效” (已解决)
近期上传应用,遇到了"此构建版本无效"的问题,如图 网查了一下,解决了这个问题:(注意:先不要急着怀疑是网络问题,重新提交,先检查问题,别问我怎么知道的...) 1:iOS10 之 ...
- 显式参数 VS 隐式参数
尽量使用显示参数,而不是隐式参数,看下面实例代码. 示例1采用显示参数,示例2采用隐式参数.对于一个不熟悉MonitorManager内部构造的调用者来说,在构造MonitorManager的时候,对 ...