Morrisda
Morrisda

Reputation: 1420

Likes in mongoDB

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

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

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.

Example of a post document:

{_id: blabla, userfrom: {}, txt: "i am the post", time: {}, geo: {}, likes: {here i have to add as many likes as users clicks, and it can exceed 16MB}}

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

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: "i am the post", time: {}, geo: {}, likes: {infinite records}}

Can someone help me dealing with it? Thank you

Upvotes: 1

Views: 1755

Answers (1)

Philipp
Philipp

Reputation: 69663

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

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.

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.

Upvotes: 5

Related Questions