将嵌入的文档填充到MongoDB / Mongoose中的本地引用

时间:2017-11-06 09:54:22

标签: node.js mongodb mongoose nosql

我对嵌套文档的引用数量感到困惑。我尝试了很多我在StackOverflow和Google上找到的解决方案,但我不能让它工作。

简而言之:场地。场地可以为各种季节定义小时的业务。例如:"游泳池于7月1日至9月15日周六和周日9至5日开放。

所以这里是(简化的)架构

Venue.find(query)
  .populate({
    path: "hours",
    populate: {
      path: "seasonref",
      model: "Venue.seasons" //tried with and without model property
    }
  }).exec((err, result) => {
    if(err) {
      console.log(err);
    }      
    // make further use of result        
  });

这是我的查询

{ _id: 5a0016882694921349cb8ee1,
    address: { ... },
    seasons:
    [ { name: 'Ganzjährig',
       from: 2016-12-31T23:00:00.000Z,
       to: 2017-12-30T23:00:00.000Z,
       _id: 5a00184c2694921349cb8eef }, //REFERENCING THIS DOCUMENT
   [length]: 1 ],
    __v: 4,
    hours:
    [ { '1':
        { opened: true,
            opening: 2017-11-06T10:00:00.000Z,
            closing: 2017-11-06T22:00:00.000Z,
            pausestart: 2017-11-06T12:00:00.000Z,
            pauseend: 2017-11-06T13:00:00.000Z,
            _id: 5a001ee62694921349cb8f2f },
        '2':
        { opened: true,
            opening: 2017-11-06T10:00:00.000Z,
            closing: 2017-11-06T22:00:00.000Z,
            pausestart: 2017-11-06T12:00:00.000Z,
            pauseend: 2017-11-06T13:00:00.000Z,
            _id: 5a001ee62694921349cb8f2e },
        ...,
        _id: 5a001ee62694921349cb8f28,
        seasonref: 5a00184c2694921349cb8eef //SHOULD BE POPULATED INTO HERE
        }
    ],
    pricecategories: [ [length]: 0 ],
    ...
}

这是我从查询中获得的示例结果

{ _id: 5a0016882694921349cb8ee1,
    address: { ... },
    __v: 4,
     seasons:
       [ { name: 'Ganzjährig',
         from: 2016-12-31T23:00:00.000Z,
         to: 2017-12-30T23:00:00.000Z,
        _id: 5a00184c2694921349cb8eef },
       [length]: 1 ],
    hours:
    [ { '1':
        { opened: true,
            opening: 2017-11-06T10:00:00.000Z,
            closing: 2017-11-06T22:00:00.000Z,
            pausestart: 2017-11-06T12:00:00.000Z,
            pauseend: 2017-11-06T13:00:00.000Z,
            _id: 5a001ee62694921349cb8f2f },
        '2':
        { opened: true,
            opening: 2017-11-06T10:00:00.000Z,
            closing: 2017-11-06T22:00:00.000Z,
            pausestart: 2017-11-06T12:00:00.000Z,
            pauseend: 2017-11-06T13:00:00.000Z,
            _id: 5a001ee62694921349cb8f2e },
        ...,
        _id: 5a001ee62694921349cb8f28,
        seasonref: {
          name: 'Ganzjährig',
          from: 2016-12-31T23:00:00.000Z,
          to: 2017-12-30T23:00:00.000Z,
          _id: 5a00184c2694921349cb8eef }
        }
    ],
    pricecategories: [ [length]: 0 ],
    ...
}

这是我想从查询中获得的示例结果

let xCoordinate: CGFloat = 1.4
XCTAssertEqual(view.frame.origin.x, xCoordinate)

无论我如何配置所有内容(使用或不使用模型,使用旧的或新的填充方法),我都不会将嵌套文档填充到结果中。

0 个答案:

没有答案