I have a mongoose model that has an association with a user model e.g.
var exampleSchema = mongoose.Schema({
name: String,
If I understood you correctly, you'll be good trying the following:
// We "copy" the request body to not modify the original one
var example = Object.create( req.body );
// Now we add to this the user id
example.userId = req.user._id;
// And finally...
var model = new Example( example );
Also, do not forget to add in your Schema options { strict: true }
, otherwise you may be saving unwanted/attackers data.
Since Node 8.3, you can also use Object Spread syntax.
var model = new Example({ ...req.body, userId: req.user._id });
Note that order matters, with later values overriding previous ones.
_ = require("underscore")
var model = new Example(_.extend({ userId: req.user._id }, req.body))
or if you want to copy userId into req.body:
var model = new Example(_.extend(req.body, { userId: req.user._id }))