-3

I have several collections: photos, photos_like , photos_comment

I have two data structure options:

Case 1: photos_like, photos_comment is a filed array in photos collection

Case 2: use relation to connect between collections

Which option should I use to get the fastest performance?

diepnt
  • 39
  • 7

1 Answers1

0

You should go for case 2 - document reference.

If you start to get a lot of traffic, you will end up having photo documents with hundreds or even thousands of comments and likes inside.

You would have a data structure like:

collection: photos

{
    "_id": ObjectId("5c53653451154c6da4623a79"),
    "name": "ocean",
    "path": "path/to/ocean.png"
}

collection: photo_comments

{
    "_id": ObjectId("c73h42h3ch238c7238cyn34y"),
    "comment": "it's actually a lake",
    "photo": ObjectId("5c53653451154c6da4623a79"),
    "user": ObjectId("sd686sd8ywh3rkjiusyyrk32")
}

collection: photo_likes

{
    "_id": ObjectId("x267cb623yru2ru6c4r273bn"),
    "photo": ObjectId("5c53653451154c6da4623a79"),
    "user": ObjectId("sd686sd8ywh3rkjiusyyrk32")
}

You can get more details at Model One-to-Many Relationships with Document References.

Caconde
  • 4,177
  • 7
  • 35
  • 32
  • Thank you. I decided to choose case 1 after reading this article: https://stackoverflow.com/questions/5373198/mongodb-relationships-embed-or-reference – diepnt Aug 21 '19 at 01:02