I'm working on a food blog. I would want to achieve the following structure for the food recipes in my database.
recipes : [
{mainCategoryRecipe1: {...}},
{mainCategoryRecipe2: {...}},
subcategory1: [
{recipe1: {...}}
]
]
I'm posting a form with select field indicating the correct category and subcategory (if needed). I can access these in the Node.js backend.
I can't figure out how to $push in to the database so the data follows the correct structure. I'm using MongoJS. My attempt on this resolves with this structure instead:
recipes : [
{subcategory1 : recipe1[...]},
{subcategory1 : recipe2[...]}
]
Here is my code:
db.collection.findAndModify({
query:{_id: mongojs.ObjectId(userId)},
update: { $push: {
recipes : {
[recipe.subcategory] : recipe
}
}},
new: true
}, function(err, doc, lastErrorObject){
if(err){
throw (err)
} else {
//console.log(recipe)
res.render('/success');
}
});
Thank you in advance!