mongodb - Mongoose Caching a Part of a Referenced Subdoc on Creation -
i have simple schema references. dont want populate referenced document on every query. instead want cache part (only single attribute) on referencing document.
a simple example schema:
user - displayname (....stuff....) posts - title - content - user (reference?)
when used references , population on demand postschema like:
var postschema = new schema({ ... user: { type: schema.objectid, ref: 'user' } })
but want save (cache) only displayname (not other stuff in user) , _id in post when created. thougts on change postschema following:
user: { _id: { type: schema.objectid, ref: 'user' }, displayname: string }
on creation of post do:
var post = new post(req.body); post.user._id = req.user._id; post.user.displayname = req.user.displayname;
the problem is: looks _id is referenced user, leading absurd user._id._id. iscreator-middleware needs convert _id string:
exports.iscreator = function(req, res, next) { if (req.post.user._id.tostring() !== req.user._id.tostring()) { return res.status(403).send('user not authorized'); } next(); };
this cannot best solution. question is: best practice case?
Comments
Post a Comment