Handling Several Booleans in a Collection in MongoDB

Hi again,

Ok so in my previous post, my #2 was actually your #1 here (if that makes any sense) :smiley: !

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 :sweat_smile:. 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 :muscle: !
Cheers,
Maxime.

2 Likes