【问题标题】:Query and map one-way referenced children and sub-children from a parent with mongoose in Keystone.js在 Keystone.js 中使用 mongoose 从父级查询和映射单向引用的子级和子级
【发布时间】:2016-09-27 15:45:02
【问题描述】:

我得到了三个具有一对多关系的模型。简单的树。我需要的是一种简单、有效的查询结构化关系树的方法,最好类似于猫鼬的 .populate() ,因为我在父模型上没有 id,所以我不能使用它。我想在父级上保留子级 ID 会很有效,但 Keystone 默认不提供此功能,我无法编写更新回调来控制关系更改。我尝试并浪费了太多时间,发现自己误入歧途,而也许我想要实现的目标要容易得多,但我就是看不到。

这是剥离的代码:

类别模型

Category.add({
    name: { type: String}
});
Category.relationship({ path: 'sections', ref: 'Section', refPath: 'category' });

部分模型,类别的子项

Section.add({
    name: { type: String,  unique: true, required: true}
    category: { type: Types.Relationship, ref: 'Category', many: false}
});
Section.relationship({  path: 'articles', ref: 'Article', refPath: 'section'});

文章模型,该部分的子项

Article.add({
    name: { type: String, required: true}
    section: { type: Types.Relationship, ref: 'Section', many: false }
});

我想获得一个包含所有子项及其各自子项的类别的结构化视图,如下所示:

[ { _id: 57483c6bad451a1f293486a0,
    name: 'Test Category',
    sections: [
        { _id: 57483cbbad451a1f293486a1,
        name: 'Test Section',
        articles: [ 
            { _id: 57483c6bad451a1f293486a0,
             name: 'Test Category' } 
        ]
    ]
} ]

【问题讨论】:

    标签: javascript node.js mongodb mongoose keystonejs


    【解决方案1】:

    所以我就是这样做的。一点效率都没有,但至少它是有效的。因为我只需要一个,所以我没有在一级父级中放任何东西。

    // Load current category
    view.on('init', function (next) {
    
        var q = keystone.list('Category').model.findOne({
            key: locals.filters.category
        });
    
        q.exec(function (err, result) {
    
            if (err || !results.length) {
                return next(err);
            }
    
            locals.data.category = result;
            locals.section = locals.data.category.name.toLowerCase();           
            next(err);
        });
    
    });
    
    
    // Load sections and articles inside of them
    view.on('init', function (next) {
        var q = keystone.list('Section').model.find().where('category').in([locals.data.category]).sort('sortOrder').exec(function(err, results) {
    
            if (err || !results.length) {
                return next(err);
            }
    
            async.each(results, function(section, next) {
                keystone.list('Article').model.find().where('section').in([section.id]).sort('sortOrder').exec(function(err, articles){
                    var s = section;
                    if (articles.length) {
                        s.articles = articles;
                        locals.data.sections.push(s);
                    } else {
                        locals.data.sections.push(s);
                    }
                });             
    
            }, function(err) {
                next(err);
            });
    
            next(err);
        });
    });
    

    但现在我遇到了另一个问题。我将 Jade 1.11.0 用于模板,有时它不会在视图中显示数据。 我将针对此问题发布另一个问题。

    【讨论】:

    猜你喜欢
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2018-02-01
    • 2015-12-16
    • 2020-07-01
    • 2021-05-30
    • 1970-01-01
    相关资源
    最近更新 更多