2

哇,这个标题好难读。好的,我被困在应该非常简单的事情上。我有一个看起来像这样的模式:

var mongoose = require('mongoose'),
  Schema = mongoose.Schema,
  TrackedVariableSchema = mongoose.model('TrackedVariable').schema;

var ChallengeSchema = new Schema({
  title: {
    type: String,
    default: '',
    trim: true,
    required: 'Title cannot be blank'
  },
  trackedVariables: [TrackedVariableSchema],
  entryConditions: [{
    trackedVar: {
      type: Schema.ObjectId,
      ref: 'TrackedVariable'
    },
    comparison: {
      type: String,
      default: '=='
    },
    compValue: String
  }]
});

mongoose.model('Challenge', ChallengeSchema);

而且,TrackedVariableSchema 看起来像:

var mongoose = require('mongoose'),
  Schema = mongoose.Schema;

var types = 'Boolean String Number'.split(' ');
var TrackedVariableSchema = new Schema({
  label: String,
  type: {
    type: String,
    enum: types,
    default: 'String'
  },
  defaultValue: String
});

mongoose.model('TrackedVariable', TrackedVariableSchema);

每个 Challenge 对象都维护一个 TrackedVariables 的内部列表。(“TrackedVariable”只是我们要为该挑战跟踪的数据的数据定义,但这不是很重要。)我更愿意将 TrackedVariables 作为其父挑战的子文档数组。我宁愿不把它们放在自己的收藏中。

我的例子有点晦涩,因为这些实体名称没有直观的意义,但希望你能看到我想要做什么。

当我 findById() 单个 Challenge 对象并希望查看每个条件的每个 TrackedVariable 的标签时,问题就出现了。像这样的东西:

{
  "_id": "A1"
  "title": "Make a Sandwich",
  "trackedVariables": [{
    "_id": "B1",
    "label": "Slices of bread",
    "type": "Number",
    "default": "0"
  }, {
    "_id": "B2",
    "label": "Has peanut butter",
    "type": "Boolean",
    "default": "false"
  }, {
    "_id": "B3",
    "label": "Has jam",
    "type": "Boolean",
    "default": "false"
  }],
  "entryConditions": [{
    "trackedVar": {
      "_id": "B1",
      "label": "Slices of bread",
      "type": "Number",
      "default": "0"
    },
    "comparison": ">=",
    "compValue": "2"
  }, {
    "trackedVar": {
      "_id": "B2",
      "label": "Has peanut butter",
      "type": "Boolean",
      "default": "false"
    },
    "comparison": "==",
    "compValue": "true"
  }, {
    "trackedVar": {
      "_id": "B3",
      "label": "Has jam",
      "type": "Boolean",
      "default": "false"
    },
    "comparison": "==",
    "compValue": "true"
  }]
}

当然,没有填充 TrackedVariables,所以我得到类似的东西:

{
  "_id": "5641adcc918df5901b1e1043",
  "title": "Make a Sandwich"
  "trackedVariables": [{
    "_id": "12345",
    "label": "Slices of bread",
    "type": "Number",
    "default": "0"
  }, {
    "_id": "23456",
    "label": "Has peanut butter",
    "type": "Boolean",
    "default": "false"
  }, {
    "_id": "34567",
    "label": "Has jam",
    "type": "Boolean",
    "default": "false"
  }],
  "entryConditions": [{
    "trackedVar": "B1",
    "comparison": ">=",
    "compValue": "2"
  }, {
    "trackedVar": "B2",
    "comparison": "==",
    "compValue": "true"
  }, {
    "trackedVar": "B3",
    "comparison": "==",
    "compValue": "true"
  }]
}

这不行,因为我需要生成一个如下所示的列表:

挑战赛报名要求:

Entry Conditions for this Challenge
===================================
Slices of bread      >=     2
Has peanut butter    ==     true
Has jam              ==     true

没问题。只需使用 populate(),对吗?

我也是这么想的。而且,如果 TrackedVariables 存储在他们自己的集合中(而不是作为挑战的内部子文档),那么这很好。但是,我尝试过的任何方法都不会给我挑战对象,其中每个 entryCondition 都填充了 TrackedVariable。

我已经尝试了几乎所有我能想到的以下排列:

Challenge.findById(id)
  .populate('entryConditions.trackedVar')
  .exec(function(err, challenge) {
  ...
...

无论我按照这些思路做什么,trackedVar 始终评估为 id 或 null(取决于我尝试的内容)。我怀疑这与以下事实有关:在响应中,trackedVariables 集合已经完全填充。而且,要在 entryConditions 集合中复制这些数据,将是……嗯……重复。

我真的很难过。似乎它应该是微不足道的。

4

0 回答 0