【发布时间】:2019-02-14 21:00:40
【问题描述】:
我最近才用猫鼬,有点困惑如何排序和分页。
假设我做了一些像 twitter 这样的项目,我有 3 个模式。第一个是用户,第二个是帖子,第三个是 post_detail。 user schema 包含用户拥有的数据,post 更像是我们可以回复的 fb status 或 twitter tweet,post_detail 就像帖子的回复
用户
var userSchema = mongoose.Schema({
username: {
type: String
},
full_name: {
type: String
},
age: {
type: Number
}
});
发帖
var postDetailSchema = mongoose.Schema({
message: {
type: String
},
created_by: {
type: String
}
total_reply: {
type: Number
}
});
post_detail
var postDetailSchema = mongoose.Schema({
post_id: {
type: String
}
message: {
type: String
},
created_by: {
type: String
}
});
关系是 user._id = post.created_by, user._id = post_detail.created_by, post_detail.post_id = post._id
假设用户 A 发表了 1 个帖子,并且有 1000 个其他用户对该帖子发表了评论,我们如何按用户的用户名对评论进行排序?用户可以更改数据(在这种情况下为全名,年龄),所以我不能将数据放在 post_detail 上,因为数据可以动态更改,或者我只是将其放在 post_detail 上,如果用户更改数据,我也只是更改 post_detail?但如果我这样做,我需要更改很多行,因为如果相同的用户评论了 100 个帖子,那么这些数据也需要更改。
问题是如何排序,我想如果我能排序我也可以分页。或者在这种情况下我应该只使用 rdbms 而不是 nosql?
无论如何,谢谢,非常感谢帮助和指导 :))
【问题讨论】:
标签: mongodb express mongoose nosql