A
A
Astralian2013-07-18 22:26:14
JavaScript
Astralian, 2013-07-18 22:26:14

Communication with sub-schema via populate()

Hello.
Such a question on MongoDB and Mongoose ORM

There are two models in Mongoose. In one, there is a sub-circuit.

var mongoose = require('mongoose'),
  Clinic = require('./clinic'),
  Schema = mongoose.Schema;
 
 
var ClinicSchema = mongoose.Schema({
  name: {
    type: String,
    trim: true,
    required: true
  },
 
  procedures: [new Schema({
    name: {
      type: String,
      trim: true,
      required: true
    },
 
    alias: {
      type: String,
      trim: true,
      required: true
    },
 
    time: {
      type: Number
    },
 
    customTime: {
      type: Number
    }
  }, {
    ref: 'Clinic'
  })]
});
 
module.exports = mongoose.model('Clinic', ClinicSchema);


var mongoose = require('mongoose'),
  Patient = require('./patient'),
  User = require('./user'),
  Clinic = require('./clinic'),
  Schema = mongoose.Schema;
 
var RecordSchema = Schema({
  doctor: {
    type: Schema.Types.ObjectId,
    ref: 'User'
  },
 
  clinic: {
    type: Schema.Types.ObjectId
  },
 
  date: {
    type: Date,
    default: new Date()
  },
 
  patient: {
    type: Schema.Types.ObjectId,
    ref: 'Patient'
  },
 
  procedures: {
    type: Schema.Types.ObjectId
  }
});
 
module.exports = mongoose.model('Record', RecordSchema);


Record stores IDs from all tables. Through ref and populate, I get all bundles except procedures.
This is apparently due to the fact that they are recorded by a sub-scheme.

The question is how can I get all records by IDs from Clinic.procedure
Record.find().populate('doctor patient').populate('procedures') from Record.procedures

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question