Hello @michael_hoeller.
I was researching and taking a look of all the docs, so allow me to share with you the conclusions for a second approach about it.
My use case is the next: I have a Users collection, but I need to keep a record of the interactions of that user with other users, in an array. Something like this:
{
"degree" : NumberInt(1),
"rating" : NumberInt(3),
"records" : [
{
"userId" : ObjectId("5f0b29c78f491172cfe8b049"),
"type" : "pending"
},
{
"userId" : ObjectId("5f0b29c78f491172ct48b077"),
"type" : "done"
}
]}
This is in case of the embedded solution. One of the main queries is to filter the Users collection and fetch those users with whom I, (The user who query) did not have any interaction yet, so I need to check in the ‘‘records’’ array that my ID is not there.
The thing is that this array can be short in some cases (40, 50) in the minor case, but it can be hundreds or thousands as well, so, the Users collection is getting queried very often (and the previous array check is not always necessary), Taking that in consideration, I thought having that array embedded is not a good idea.
So, I think the other solution is to have the Records in another collection, One to One relationship and made the query via $lookup (I already tested in Compass and it’s possible, it works).
The records collection will look something like:
{
"_id" : ObjectId("5f0fb901b320f5ec21269279"),
"userId" : ObjectId("5f0b29c78f491172cfe8b04a"),
"record" : [
{
"user" : ObjectId("5f0b29c78f491172cfe8b049"),
"name" : "Gabriel",
"type" : "progress"
},
{
"user" : ObjectId("5f0b29c78f491172cfe8b04b"),
"name" : "Rivaldo",
"type" : "sended"
}
]
}
Also it allows me to add more fields or modify the Records schema if the specificities of the project changes, (very high probability) without worry about growing, or modifying the users collection too often, since it is the most important collection of the project.
What are your thoughts about it? Thanks in advance!
Regards, Gabriel