I am storing a string in a database along with the owners of the string (one or more owners per string).
I’ve always worked with MySQL which is a conventional relational database. In that case, I would store the string along with a unique id in one table, and then the unique id of the string along with the owners (as multiple records) in a second table.
I could then fetch strings by owners using a SQL Join.
I am now working on a project using MongoDB, and I’m doing the same as above.
Would this be considered the wrong way when working with NoSQL databases? Should I not be thinking in terms of ‘relations’ when working with NoSQL?
Another way I can think of achieving the same in MongoDB is storing it like this:
{
"string": "foobar",
"owners": [
"owner1",
"owner2",
"owner3"
]
}
However, in this case, I’m unsure how I would search for “all strings owned by owner1”.
This looks like the correct approach; remember though, it always depends on the totality of your project, what the goals are (performance, flexibility), what queries you intend to run most heavily, if you need to run ad-hoc queries, and other factors. In general though, using nested documents, as you wrote, is the correct alternative to using joins and foreign keys.
Keep in mind also the maximum document size (currently 16MB), which would be a concern if there are many (like, hundreds of thousands) of owners of a given string.