最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mongoose - Child Schema References Parent Subdocument - Stack Overflow

programmeradmin5浏览0评论

Is it possible to have a Mongoose Schema that resembles the following:

var categorySchema = new Schema({
    name : String
});

var childSchema = new Schema({
   name : String,
   category : {
      type : Schema.Types.ObjectId,
      ref : 'parent.categories'
   }
});

var parentSchema = new Schema({
    categories : [categorySchema],
    children : [childSchema]
});

Basically a child can only have a category that is contained by its parent. Is what I am trying to do possible? If not what is the cleanest way to do this?

Is it possible to have a Mongoose Schema that resembles the following:

var categorySchema = new Schema({
    name : String
});

var childSchema = new Schema({
   name : String,
   category : {
      type : Schema.Types.ObjectId,
      ref : 'parent.categories'
   }
});

var parentSchema = new Schema({
    categories : [categorySchema],
    children : [childSchema]
});

Basically a child can only have a category that is contained by its parent. Is what I am trying to do possible? If not what is the cleanest way to do this?

Share Improve this question asked Feb 29, 2016 at 2:15 fqhvfqhv 1,2011 gold badge13 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

If there is only one field name in categorySchema, maybe you could just put it into parentSchema without population as below,

var childSchema = new Schema({
   name : String,
   category : {
      name: String
   }
});

var parentSchema = new Schema({
    categories : [{name: String}],
    children : [childSchema]
});

When try to insert new child into parent, you could query the parent firstly, then iterate categories to get existing one and add it to children, save the parent as last, sample codes as below

Parent.find({_id: parent._id})
      .exec(function(err, p) {
          if (err) throw err;
          var p = new Child({name: 'tt'});
          p.categories.forEach(function(c) {
              if (c /*find the match one*/) {
                  p.category = c; // assign the existing category to children
              }
          });
          // save this parent
          p.save(function(err) {...});
      });  

If there are many fields in categorySchema, maybe define it as individual schema could be one option, in case of there are many categories in Parent to make parent collection too large.

var categorySchema = new Schema({
    name : String,
    // other fields....
});
var Category = mongoose.model('Category', categorySchema);

var childSchema = new Schema({
   name : String,
   category : {type : Schema.Types.ObjectId, ref : 'Category'}
});

var parentSchema = new Schema({
    categories : [{type : Schema.Types.ObjectId, ref : 'Category'}],
    children : [childSchema]
});

The same logic when try to add new children to parent document as above.

发布评论

评论列表(0)

  1. 暂无评论