把前端页面都搭好了之后就要开始后端的设计了,后端使用mongoose在数据库中实现增删改查功能。主要一点就是数据库的设计。
第一次设计数据库,要为每个用户建一个表,也要为所有的文章加一个表。
mongodb是nosql类型的数据库,设计思想是面向对象,比如在用户表里就要想,一个用户需要什么数据;同样在文章表就需要思考一个文章需要什么关联的数据。尽可能将所有关联的数据都放在一个表里,这也是nosql数据库的优点。只需要一次查询,更加高效。
在设计scheme的时候,要注意文档的树形结构,层级关系要分清。
下面是我的数据库表设计:
1、文章表的scheme:
var mongoose = require('mongoose');
var articleSchema = new mongoose.Schema({
"articleId":String,
"articleTitle":String,
"articleGoTime":String,
"articleGoDay":String,
"articlePublishTime":String,
"articleDestination":String,
"articleAveragePrice":String,
"articleOwnerId":String,
"articleOwnerName":String,
"articleOwnerSchool":String,
"articleOwnerSex":String,
"articleDesciption":String,
"articleImg":String,
"articleComment":[
{
"commentId":String,
"commentContent":String,
"commentOwnerName":String,
"commentOwnerSchool":String,
"commentToWhom":String,
"commentPublishTime":String,
}
],
});
module.exports = mongoose.model("Article",articleSchema);
2、用户表
var mongoose = require('mongoose');
var userSchema = new mongoose.Schema({
"userId":Number,
"userName":String,
"userSex":String,
"userPwd":String,
"userNickName":String,
"userSchool":String,
"userHeadImg":String,
"publishArticle":[
{
"articleId":String,
"articleGoTime":String,
"articleDestination":String,
"articleDescription":String,
/*自己文章的评论*/
"comment":[
{
"time":Date,
"from":String,
"articleId":String,
"commentContent":String
}
]
}
],
"collectArticle":[
{
"articleId":String,
"articleGoTime":String,
"articleDestination":String,
"articleDescription":String,
"articleCommentNumber":String
}
],
/*我的私信*/
"message":[
{
"with":String,//nickName
"myMessageContent":[ //我发送的多条私信
{
"name":String,
"sendTime":Date,//根据时间排列渲染
"content":String
}
],
"hisMessageContent":[ //对方发送的多条私信
{
"name":String,
"sendTime":Date,
"content":String
}
]
}
],
/*文章里给我的回复*/
"toMeReply":[
{
"time":Date,
"from":String,
"replyContent":String,
"articleId":String
}
]
});
module.exports = mongoose.model("User",userSchema);