I joined this new project that is using MongoDB, and I saw this structure:
collection: Users
salutation (ObjectId) name (String) status (ObjectId)
ObjectId("111") Mark ObjectId("112")
collection: Values
_id description (String)
ObjectId("111") Mr.
ObjectId("112") active
For all the values that are not changeable, they are referring to the id of the Values collection. I don't know if it's a common practice in the relational data bases, but I haven't seen it in MongoDB so far.
They use Nestjs and Mongoose.
For each request they need to query Users collection and multiple times Values collection. Is this a good approach?
CodePudding user response:
Please avoid the use of a Values collection, which is not a good standard in mongo. If you want a list of valid salutation values you can create a Salutation Collection and so on.
Nevertheless, all will depend on the use case. Sometimes you will be able to nest attributes and sometimes you will need to have a different collection and point to that new collection with an object id.
You can read about all the mongo design patterns and every use case here: https://www.mongodb.com/blog/post/building-with-patterns-a-summary
I feel that in this case, you should nest the values of the user in the user collection and that will be fine
You can have something like this:
{
"name": "Derek",
"status": "active",
"salutation": "Mr."
}
If not you will need to do lookups in a way to get the values and that will not have a good performance.