Answer the question
In order to leave comments, you need to log in
How can I update a populated field with Mongoose?
Good afternoon! The question will be based on a fairly simple example. There are such models:
var mongoose = require('mongoose')
, Schema = mongoose.Schema
var storySchema = Schema({
title : String,
author : { type: Schema.Types.ObjectId, ref: 'Author' }
});
var authorSchema = Schema({
name : String,
age: Number
});
var Story = mongoose.model('Story', storySchema);
var Author = mongoose.model('Author', authorSchema);
var newStory = new Story({
title: story_title,
author: author_id
});
newStory.save(function(err){
if(err) throw err;
...
});
Story.findOneAndUpdate({ id:story_id }, { title: story_title, author: author_id }, function(err, repertoire){
if(err) throw err;
...
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question