node.js - Mongoose, is it possible to assign parent id to sub document on save in one call? -


i wondering if it's possible(or needed) provide subdocument reference it's parents id in 1 call. here code i'm working with:

var mongoose = require('mongoose'); var schema = mongoose.schema;  var latlng = new schema({   id: schema.objectid,   created_at: date,   accuracy: {     type: number,     required: true   },   latitude: {     type: number,     required: true   },   longitude: {     type: number,     required: true   },   _walk: {     type: schema.objectid,     ref: 'walk',     required: true   } }); latlng.pre('save', function(next){   if(!this.created_at)     this.created_at = new date(); });  var walk = new schema({   id: schema.objectid,   created_at: date,   updated_at: date,   description: string,   elapsedtime: number,   distance: number,   waypoints: [latlng],   _user: {     type: schema.objectid,     ref: 'user',     required: true   } });  walk.pre('save', function(next){   var = new date();   this.updated_at = now;   if(!this.created_at)     this.created_at = now;   next(); });  walk.pre('update', function(next){   this.updated_at = new date(); });  walk.pre('findoneandupdate', function(next){   this.updated_at = new date(); });  walk.pre('findbyidandupdate', function(next){   this.updated_at = new date(); });  module.exports = mongoose.model('walk', walk); 

wondering if there's way, maybe in latlng.pre('save'), assign latlng._walk walk.id?

so when like:

var walk = new walk({     description: req.body.description,     elapsedtime: req.body.elapsedtime,     distance: req.body.distance,     waypoints: req.body.waypoints,     _user: req.user._id   }); 

it allow me call walk.save() , not have iterate through waypoints , manually assign _walk walk._id

i hope makes sense, help!

if need why not manually create before , save document it. can generate objectid instead of letting mongo handle it. there nothing against that.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -