node.js - Mongoose- How to reference embed document element? -


i have users.js schema embeded document array pets. each user, user can have multiple pets(usually no more 3 think).

for each pet, there daily chart. many daily charts pet. have read embedded documents each array element indexed. in daily.js, how can reference pet belong populate() function?

var mongoose = require('mongoose'); var schema = mongoose.schema;  var userschema = new schema({     firstname: { type: string, required: true },     lastname: { type: string, required: true },     username: { type: string, required: true, unique: true },     location: string,     pets: [{ name: 'string', animaltype: 'string'}], //could have more 1 pet     created_at: date,     updated_at: date }); 

var mongoose = require('mongoose'); var schema = mongoose.schema;  var dailyschema = new schema({   tite: string,   _pet: { type: number, ref: 'user.pet' }, // not sure how reference name in user.pets[#] array   created_at: date,   updated_at: date }); 

quoting

sorry disappoint anti-pattern. populate can't populate collection's subdocs - reason why you're getting error there's no model boards.

so may not patten reference embedded document. better separate pet user 1 schema

var petschema = new schema ({      name: 'string',      animaltype: 'string' }); 

and userschema , dailyschema be

var userschema = new schema({     ...     pets: [{ type: schema.types.objectid, ref: 'pet' }], //could have more 1 pet });  var dailyschema = new schema({   _pet: { type: number, ref: 'pet' }, // not sure how reference name in user.pets[#] array }); 

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -