Skip to content

Instantly share code, notes, and snippets.

@bitdewy
Created March 12, 2014 17:30
Show Gist options
  • Save bitdewy/9511929 to your computer and use it in GitHub Desktop.
Save bitdewy/9511929 to your computer and use it in GitHub Desktop.
xocean schema
// !!! sketch, no constraint
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var WorkItemSchema = new Schema({
description: String,
createdAt: Date,
lastModified: Date,
status: Number
});
// no comment on comment so far
var CommentSchema = new Schema({
username: String,
content: String
});
var TagSchema = new Schema({
name: String,
description: String
})
var ArticleSchema = new Schema({
title: String,
body: String,
createdAt: Date,
lastModified: Date,
comments: [CommentSchema],
tags: [TagSchema],
status: Number
});
var UserSchema = new Schema({
name: String,
email: String,
role: {
type: String,
default: 'user'
},
hashedPassword: String,
provider: String,
salt: String,
facebook: {},
twitter: {},
github: {},
google: {},
group: Number,
employeeID: String,
works: [WorkItemSchema],
articles: [ArticleSchema]
});
@ijse
Copy link

ijse commented Mar 13, 2014

https://gist.github.com/bitdewy/9511929#file-xocean-db-js-L29-L30
这两个字段,根据业务需求,简单存成nested的就可以了

https://gist.github.com/bitdewy/9511929#file-xocean-db-js-L43
是不是可以不存salt? 用户密码加密可以采用password + userSalt + globalSalt 形式

另外,还缺不少字段 。。

@bitdewy
Copy link
Author

bitdewy commented Mar 13, 2014

https://gist.github.com/bitdewy/9511929#file-xocean-db-js-L29
comment 同意 nested. (原本考虑为 comment on comment 做准备的)

https://gist.github.com/bitdewy/9511929#file-xocean-db-js-L30
tag 与 article 不是 one-to-many 的关系,是 many-to-many,通过 tag 应该可以查找 article,建议保留,改成 many-to-many 的~

UserSchema,是从现在的代码里面搬运过来的,= =~

其他的想不起来了~

@meterler
Copy link

建议TagSchema 增加出现次数,以后可以做个热词统计。

@ijse
Copy link

ijse commented Mar 13, 2014

@bitdewy

comment on comment 用nested也同样可以的,只要我们不需要单独处理comment

MongoDB是文档型数据库,与其它关系型数据库在设计时是有些不同的,按关系型数据库思想去设计,你会很痛苦的。

tag用 String 或者 [String]比较合适,不仅可以减少一次查询请求,而且读写也更方便。

写方便就不说了,查询可以像下面这样:

articleCollection.find({ tag: new RegExp('\bsearch_tag\b', 'i') });

或者,对于[String]类型的:

articleCollection.find({ tag: { $elemMatch: 'search_tag' } });

同样对于查询所有tag也是可以只用一次查询就做到的。

mongoose虽然是orm框架,但对于两个Collection的关联并不会自动给你处理,MongoDB的DBRef基本约等于手动。

@meterler
Copy link

var WorkItemSSchema = new Schema({
    createdAt: Date,
    lastModified: Date,
    summaryItems:[WorkItemSchema],
    planItems:[WorkItemSchema]
});

var WorkItemSchema = new Schema({
    description: String,
    status: Number
})

工作项那块可能得复杂一点

@bitdewy
Copy link
Author

bitdewy commented Mar 13, 2014

@ijse 懂了~ 原则就是能在一个 collection 里就尽量在一个 collection 里, 文档型想怎么嵌套怎么嵌套, 随意度有点高啊~

gist 是没有 notification ??!!! (╯‵□′)╯︵┻━┻

@bitdewy
Copy link
Author

bitdewy commented Mar 13, 2014

@meterler 任务项有必要打包到一起? 根据上次的讨论, 有人提到有可能一周发多次, 所以工作项设计成单独的, 这样我们可以根据创建的时间段帮助用户打包.

如果决定打包到一起的话, 这个也要接受 @ijse 的意见, 文档型嘛, 直接嵌套不用拆了.

@meterler
Copy link

@bitdewy
那还是单独的吧,不过还是得加个字段区分下上周还是下周吧

@ijse
Copy link

ijse commented Mar 14, 2014

@bitdewy

不是那么简单的原则。。 其实我觉得设计的时候需要各种权衡,最重要的是要跟业务结合,有过一些经验,设计的时候就可以避免走一些弯路。

可以去参考下其它开源项目的源码,很多的。

那个数据库设计的三泛式其实在这里不是特别适合。

@ijse
Copy link

ijse commented Mar 14, 2014

@meterler @bitdewy

相关xOcean的讨论,以后还是到teambition上吧,这样也便于以后整理和回顾。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment