Suppose I am looking to keep track of a list of items for each user (using MongoDB with Mongoose ODM in a Node.js environment) and later query to determine if an item belongs to a specific user. For instance, I wish to store all the favorite colors of each user and then check if a particular color is associated with a certain user. It appears to me that it would be more efficient to store the colors as an embedded object within the user document rather than as an array within the user document. This is because checking whether a color exists in an object can be done by simply verifying if the object property exists:
if(user.colors.yellow){
//true case
} else {
//false case
}
As opposed to using an array where one would need to iterate through the entire array to confirm if the color exists somewhere within it:
for (var i = 0; i < user.colors.length; i++) {
if(user.colors[i] === "yellow"){
//true case
} else {
//false case
}
}
However, based on several examples I've come across online, it seems that using arrays for this purpose is quite common. Am I overlooking something? What are the advantages and disadvantages, and what is the most suitable approach?