update schema in mongoose to add new property
If I'm understanding this right, you're trying to replace the creator
field with the user
field. To accomplish this you'll need to do 2 things:
- Modify your
/addDrink
route - Update any existing documents in the
drinkList
collection
1) When you create newDrink
, set user
instead of creator
:
let newDrink = new UserDrinks({ user: req.body.creator, caffeine: req.body.caffeine, mgFloz: req.body.mgFloz, name: req.body.name, size: req.body.size, updated_at: req.body.updated_at});
2) Update all UserDrinks
to rename the field:
UserDrinks.update({}, { $rename: { "creator" : "user" } }, { multi: true }, callback)
This will find all UserDrinks documents and rename the creator
property to user
.
A quick way to do this without having to create a new route for a one-time change is to create a temporary function for it under your express setup, run it locally, and delete/comment the code after so it doesn't run again. Ex:
function renameCreatorField() { UserDrinks.update({}, { $rename: { "creator" : "user" } }, { multi: true }, function(err, data) { if (!err) { //success } })}renameCreatorField();
This will run immediately when your server starts up. Once it runs once you can delete it because the changes we made in (1) will ensure any future UserDrinks documents have the correct field set.
Of course, if you're just looking for a quick fix and this isn't a live application you can just do (1), clear out your database, and start fresh with correct documents. But that's not the way to learn. :)