邮件和共享的Mongodb模式 [英] Mongodb Schema for Posts and Shares

查看:75
本文介绍了邮件和共享的Mongodb模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mongodb NoSQL 概念的新手,并且陷入无法为最适合我的目的的模式建模做出决定的时刻.

I am new to mongodb NoSQL concept and stuck at point where I am unable to take a decision for modelling the schema that could best serve my purpose.

我需要以这样的方式设计架构,使最终结果为按时间排序的帖子和共享.为此,我考虑了两个选择:

I need to design schema in such a way that I have my end result as Posts and Shares sorted by time. For this I considered two options:

选项1:帖子和共享的不同集合为:

帖子收集模式:

var postSchema = mongoose.Schema({
   postText: String,
   postedBy: String, 
   privacy: Number,
   updatedOn: { type: Date, default: Date.now }        
}, { collection: 'posts' }); 

共享收集方案

var shareSchema = mongoose.Schema({
   dis_Id: { type: mongoose.Schema.Types.ObjectId }, // Id of post that is shared
   shareBy: { type: mongoose.Schema.Types.ObjectId },
   shareText: String,
   share_privacy: Number,
   shareOn: { type: Date, default: Date.now }
}, { collection: 'shares' });


选项2:将共享本身嵌入到帖子中

新的发布架构

var postSchema = mongoose.Schema({
  postText: String,
  postedBy: String,
  updatedOn: { type: Date, default: Date.now }, 
  privacy: Number,
  share: {
    shareBy: { type: mongoose.Schema.Types.ObjectId },
    shareText: String, 
    share_privacy: Number,
    shareOn: { type: Date } 
  }       
}, { collection: 'posts' });


现在哪一个是更好的选择?选项1在查询中存在问题,因为mongodb中没有连接,而选项2将导致相同数据的复制,并且对于成千上万的用户而言可能增长到数十亿.


Now which of this could be a better choice? Option 1 has problem in querying as there are no joins in mongodb and Option 2 will lead to replication of the same data and can grow up to more than billions for hundreds of thousands of users.

推荐答案

当您将所有必需的数据汇总在一起时,使用嵌入式文档很容易,因此在这种情况下,选项2很好.但是,如果您担心文档的大小增加到16MB以上,请选择选项1.在这种情况下,请勿使用聚合查询来连接两个集合,因为这将是一项耗时的操作,因为它将首先执行所有操作,然后执行跳过操作.相反,您应该单独查询每个集合,并使用一些自定义逻辑自己创建完整的响应.

Well it is easy to work with embedded documents as you get all the required data together so option 2 is good in that case. But if you are concerned about the size of documents increasing more then 16MB then go with option 1. In that case do not use aggregate query to join two collections because that will be time consuming operation became it will first perform all the operations and then perform skip operation. Instead you should query on each collection individually and create a complete response yourself with some custom logic.

这篇关于邮件和共享的Mongodb模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆