【问题标题】:How nested query relational data in keystone.js(mongoose)?如何在 keystone.js(mongoose) 中嵌套查询关系数据?
【发布时间】:2015-02-08 07:35:28
【问题描述】:

这是我的数据结构,我想实现一个关系查询,但是查询的是嵌套的数据字段。我看了mongoose文档后知道嵌套查询的基本用法,但是我测试的时候却没有返回。请帮帮我,我将不胜感激。

//Define
var TimeLine = new keystone.List('TimeLine', {
    hidden:true
});
TimeLine.add({
    article:{type:Types.Relationship,ref:'Post'}
});

var Post = new keystone.List('Post', {
    map: { name: 'title' },
    autokey: { path: 'slug', from: 'title', unique: true }
});

Post.add({
    title: { type: String, required: true },
    state: { type: Types.Select, options: 'draft, published, archived', default: 'draft', index: true },
    author: { type: Types.Relationship, ref: 'User', index: true },
    publishedDate: { type: Types.Date, index: true, dependsOn: { state: 'published' } },
    image: { type: Types.CloudinaryImage },
    content: {
        brief: { type: Types.Html, wysiwyg: true, height: 150 },
        extended: { type: Types.Html, wysiwyg: true, height: 400 }
    }
});
//Usage

var timeline = keystone.list('TimeLine').model;
    timeline.find({
        "article.title":"xxxxxxxxx"//Here is a nested query
    }).populate('article').exec(function (err, result) {
        console.log(err,result);//This is the query results, but it can not return anything.
    });

//nothing return!!!

【问题讨论】:

    标签: javascript node.js mongodb mongoose keystonejs


    【解决方案1】:

    很遗憾,您想要完成的任务在 Mongoose 中是不可能的。 MongoDB 或 Mongoose 中没有连接。填充引用是最接近的等价物。

    填充关系时,Mongoose 首先解析查询,然后根据在初始查询中找到的文档填充引用的集合。这意味着您不能在初始查询中引用相关集合中的字段。

    在您的示例中,您尝试在填充 article 集合之前引用 article 集合的 title 字段。

    timeline.find({
        "article.title":"xxxxxxxxx" // <<-- invalid reference
    }).populate('article') ...
    

    【讨论】:

      猜你喜欢
      • 2021-04-11
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 2017-06-19
      相关资源
      最近更新 更多