Что такое альтернативный метод виртуального заполнения для подсчета документов и как влияет виртуальное заполнение на модель? - PullRequest
1 голос
/ 18 мая 2019

Я хочу подсчитать общее количество водителей в конкретном городе, поэтому я использую виртуальное заполнение, просто хочу узнать альтернативный метод и какое-либо влияние на модель?

const DriverSchema = new Schema({
  name: String,
  band: String,
  cityId : {
      type: mongoose.ObjectId,
      ref: 'city'
  }
});

const CitySchema = new Schema({
  name: String
});

CitySchema.virtual('totalMember', {
  ref: 'Driver', // The model to use
  localField: '_id', // Find people where `localField`
  foreignField: 'cityId', // is equal to `foreignField`
  count: true // count document
});

const Driver = mongoose.model('driver', DriverSchema);
const City = mongoose.model('city', CitySchema);


City.findOne({})
    .populate('totalMember')
    .exec((error, city) => {
        city.totalMember
    }); 
...