node.js - Reference item in sub-schema of model in Mongoose ODM -
im setting mongodb models, , have 1 models schema (partition model) setup in such way 1 of schema items (fields) array of items follows schema (field schema)
here's partition model (with partition schema , field schema):
// partition model module.exports = mongoose => { const schema = mongoose.schema // field schema const fieldschema = new schema({ name: { type: schema.types.string } }) // partition schema const partitionschema = new schema({ name: { type: schema.types.string }, // `fields` array of objects must follow `fieldschema` fields: [ fieldschema ] }) return mongoose.model( 'partition', partitionschema ) } then have model (asset model), has attributes array, holds objects each have 2 items, _field , value. _field needs id reference item in partition models fields._id values.
heres asset model:
// asset model module.exports = mongoose => { const schema = mongoose.schema const assetschema = new schema({ attributes: [{ // attributes._field should reference 1 of partition field values _field: { type: schema.types.objectid, ref: 'partition.fields' // <-- line }, value: { type: schema.types.mixed, required: true } }], // reference partition id asset belongs _partition: { type: schema.types.objectid, ref: 'partition' } }) return mongoose.model( 'asset', assetschema ) } where i'm running issues, _field item in asset schema. im not sure should set ref value, since referencing sub-schema (meaning field schema within partition schema)
i may have overlooked in docs, didn't see anything. how can reference models sub-schema, when populate item in query, populates sub-documents inside partition model documents?
ive tried reference field documents partition.fields, resulted in error:
missingschemaerror: schema hasn't been registered model "partition.fields".
i tried above ref value based on read thread, doesn't seem work.
apparently, isn't possible.. ill resort creating model
Comments
Post a Comment