【问题标题】:How to add an objectID to recursively embedded subdocuments如何将 objectID 添加到递归嵌入的子文档
【发布时间】:2021-04-17 01:50:43
【问题描述】:

我对@Guilherme here 提出并解决的问题有所不同,但我的递归嵌入文档位于另一个类似这样的架构中;

var mongoose = require('mongoose');

var CollectPointSchema = new mongoose.Schema({
  name: {type: String},
  collectPoints: [ this ]
});

var GroupSchema = new mongoose.Schema({
    label: {type: String},
    points: [CollectionPointSchema]
});

const Group = mongoose.model("Group", GroupSchema);

我想修改 Guilherme 提出的here 的解决方案,但不知道如何去做。

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    主要问题是子文件夹没有填充名称:字段。我认为是因为该字段不在架构的顶层。因此,作为一种解决方法,我已将 name: 字段添加到父模式中,如下所示;

    var GroupSchema = new mongoose.Schema({
        label: {type: String},
        name: {type: String},
        points: [CollectionPointSchema]
    });
    

    我还需要从这里更改 Guilherme 解决方案的顺序;

    var FolderModel = mongoose.model('folders', FolderSchema);
    
    FolderSchema.pre('save', function(next) {
      if (this.isNew) {
        recursive_reference(FolderModel, this, "folders")
      }
    
      next();
    });
    

    到这个;

    FolderSchema.pre('save', function(next) {
      if (this.isNew) {
        recursive_reference(FolderModel, this, "folders")
      }
    
      next();
    });
    
    var FolderModel = mongoose.model('folders', FolderSchema);
    

    结果是我在父级有一个未使用的字段,但它可以工作。

    【讨论】:

      猜你喜欢
      • 2012-04-12
      • 2011-12-20
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多