MongooseJS - How to save document and referenced document
Date : March 29 2020, 07:55 AM
Any of those help Check the 'Updating Refs' section of the Mongoose documentation on populate. You will see there, in the example, that you still must save both the Request and User documents you have created. (In the example, they first save the user 'guille' then in the callback they save the 'story'.)
|
mongoosejs pre save hook on array field
Date : March 29 2020, 07:55 AM
it should still fix some issue map() doesn't modify the original array, you need to assign the result to the property: ArticleSchema.pre('save', function(next) {
this.categories = _.map(this.categories, function(category) {
console.log(category.toLowerCase());
return category.toLowerCase();
});
next();
});
|
MongooseJS Pre Save Hook with Ref Value
Date : March 29 2020, 07:55 AM
it should still fix some issue You'll need to do another query, but it's not very hard. Population only works on a query, and I don't believe that there is a convenience hook for such cases. var User = mongoose.model('User');
TopicSchema.pre('save', function(next) {
var self = this;
User.findById( self.user, function (err, user) {
if (err) // Do something
var usersTime = moment().tz(user.timezone).hours(0).minutes(0).seconds(0).milliseconds(0); // Reset the time to midnight
var nextNotifyDate = usersTime.add(1, 'days').seconds(self.timeOfDay); // Add a day and set the reminder
self.nextNotificationDate = nextNotifyDate.utc();
next();
});
});
|
Populate document if there is no value, using MongooseJS
Date : March 29 2020, 07:55 AM
|
mongoosejs upsert with on save hook
Date : March 29 2020, 07:55 AM
|