node.js - Mongoose.js - population and additional fields? -
i want add additional data usermodel watchedmovies , have following schema:
let userschema = new schema({ watchedmovies: [{ type: schema.types.objectid, ref: 'movie' }] })
and:
let movieschema = new schema({ title: string })
i wondering possible add additional fields watchedmovies object , store along objectid? add watchedat date when i'm populating watchedmovies usermodel.find().populate('watchedmovies', 'title').exec(...)
like:
{ _id: objectid(userid), watchedmovies: [{ _id: objectid(...), title: 'some title', watchedat: timestamp }] }
watchedat attribute specifies when (date object) reference added usermodel
is possible mongoose , how should change schema?
thank you
change schema to
let userschema = new schema({ watchedmovies: [{ movie: { type: schema.types.objectid, ref: 'movie' }, watchedat: date }] })
then can populate .populate('watchedmovies.movie')
Comments
Post a Comment