喜欢在mongoDB [英] Likes in mongoDB

查看:143
本文介绍了喜欢在mongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我的问题:我想创建一个网络,允许用户上传帖子和喜欢他们。

Here my problem: I'd like to create a network which allows users to upload posts and like them.

我想我可以将每个帖子存储在一个名为post的集合中,我没有问题。

I think i can store each post in a single collection called 'post', and i don't have problems in doing it.

但是在哪里可以存储喜欢每个与相关数据(时间,用户等)。
我不能在post文档内,因为文档的最大大小是16MB,想象我将记录数千个喜欢与相关数据的每个帖子,我不能这样做。

But where can i store likes on each post with related data (time, userfrom etc..)? I can't do it inside post document because maximum size of a document is 16MB, and imagining i'll have to record thousands of likes with related data for each post i can't do this.

我可以将每个帖子关联到一个集合,其中包括文档和每个文档都是一样的。但我真的不想创建数百万和数百万的集合,如果可能的话。

I could relate each post to a collection, which includes documents and each document is a like. But i honestly prefer not to create millions and millions of collection if possible.

文章范例:

{_ id:blabla,userfrom: },txt:我是帖子,时间:{},geo:{},喜欢:{这里我必须添加与用户点击次数相同的喜好,可以超过16MB}}

现在在'likes'我可以引用一个集合,但这是我不想做的。

now in 'likes' i can put a reference to a collection, but this is what i'd like not to do.

我读了关于gridstore在mongodb,但我没有得到的点。我可以使用它作为一个文件,大小可以趋向无限?所以,我可以添加多少我想要的记录?所以使用网格存储可以做如下:

I read about gridstore in mongodb but i didn't get the point. can i use it as a document which size can tend to infinite? So that i can add as many records as i want? So with grid store can i do something like:

{_ id:blabla,userfrom:{},txt:我是帖子时间:{},geo:{},喜欢:{infinite records}}

有人可以帮我处理吗?谢谢

Can someone help me dealing with it? Thank you

推荐答案

当你不想嵌入喜欢的内容时,你必须走关系。

When you don't want to embed the likes, you have to go the relational way.

创建另一个集合喜欢,其中每个文档都有一个字段,说明它适用于哪个帖子。为了避免必须计数likes-collection中的所有条目以获取帖子的喜欢数,您还应该将当前计数存储在后文档中。

Create another collection "likes" where each document has a field which says which post it applies to. To avoid having to count all entries from the likes-collection to get the number of likes of a post, you should also store the current count in the post-documents.

关于GridFS:我不认为它会帮助你在这里。它不应该用于文档。其目的是在数据库中存储大型二进制文件。

Regarding GridFS: I don't think it will help you here. It is not supposed to be used for documents. Its purpose is to store large binary files in the database.

这篇关于喜欢在mongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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