Assume that you have a “users” and “logs” collections. A user can have many logs. As you can imagine, this is a mutable operation and you cannot guarantee how many logs a user could potentially have. It may be 1 or a million! In such cases it is ideal to store parent collection’s reference in child collection. So in this case the log contains user reference in it. See example below.

Database content

Users data
Logs data

HTTP Router


#go #mongodb #golang

MongoDB one-to-many relationships to keep parent document reference in childdocument in Go
1.45 GEEK