Hi again,
Ok so in my previous post, my #2 was actually your #1 here (if that makes any sense)
!
So now, with your numbers, here is my opinion - which is just an opinion, maybe itâs not perfect - but here it is.
#1 is terrible. Impossible to index to say the least. Also Mongoose is kinda an anti-pattern as it forces a schema over a schemaless/schemafree database
. Again - just an opinion - but I find these tools (mongoose, Spring Data MongoDB, âŠ) a bit counter productive. They bring more complexity than they save time & I honestly prefer to stick to the plain JS, Java or Python driver without an extra âproxyâ.
#2 indexes would depend on how you are running the queries but I donât see the âprosâ of this schema design. You would need to populate all the cities in the array while in the #3 solution you just store the cities that people visited - which is - I guess - a smaller portion of the actual list for each people. In term of storage I think #3 is way easier to query (and index) and saves more storage.
Regarding this:
Your front-end implementation shouldnât be THAT coupled to the back-end implementation. The computation should be done in the back-end and hide the actual implementation. The REST or GraphQL interface that the front-end consumes doesnât have to be impacted even if you choose #1 over #3⊠But to serve that âconvenientâ API to the front-end, you would, indeed, require more or less computation & simple or hard queries in the back-end to build the answer for the front-end.
If itâs easier for the front-end to build the UI with
{
"name": "Bob",
"London": true,
"Paris": false,
...
}
Then you should return this in your API. But that doesnât prevent you from storing the data in your collection in an array of strings and compute the transition in your back-end.
By the way, #3 requires to store somewhere the list of all the cities (metadata doc) and this also suggests that this list needs a caching system so you donât read this metadata doc every 3”s when itâs actually only updated every 6 weeks. Iâm going to invent a new proverb just for you!
1 query is better than 2!
Iâm glad this was somewhat useful to you
!
Cheers,
Maxime.