mongodb - Mongoose:深人口(填充人口密集的领域)

我有 Category 模型:

Category:
    ...
    articles: [{type:ObjectId, ref:'Article'}]

文章模型包含对帐户模型的引用。

Article:
    ...
    account: {type:ObjectId, ref:'Account'}

因此,使用填充的 articles 类别模型将是:

{ //category
    articles: //this field is populated
     [ { account: 52386c14fbb3e9ef28000001, // I want this field to be populated
         date: Fri Sep 20 2013 00:00:00 GMT+0400 (MSK),
         title: 'Article 1' } ],
    title: 'Category 1' }

问题是:如何填充已填充字段([articles])的子字段(帐户)?这是我现在的做法:

globals.models.Category
    .find
        issue : req.params.id
        null
        sort:
            order: 1
    .populate("articles") # this populates only article field, article.account is not populated
    .exec (err, categories) ->
        console.log categories

我知道这里讨论过:Mongoose: Populate a populated field但没有找到真正的解决方案

最佳答案

首先,将 mongoose 3 更新为 4,然后使用最简单的方法在 mongoose 中进行深度填充,如下所示:

假设您的博客架构具有 userId 作为 ref Id,然后在 User 中您有一些评论作为架构 Review 的 ref Id。所以基本上,你有三个模式:

  1. 博客
  2. 用户
  3. 评论

而且,您必须从博客中查询,哪个用户拥有此博客和用户评论。 所以你可以查询你的结果:

BlogModel
  .find()
  .populate({
    path : 'userId',
    populate : {
      path : 'reviewId'
    }
  })
  .exec(function (err, res) {

  })

https://stackoverflow.com/questions/18867628/

相关文章:

mongodb - 在 mongodb 中使用 findOne 获取具有最大 id 的元素

mongodb - 查询 MongoDB 的 IDE?

mongodb - 仅返回嵌套数组中匹配的子文档元素

mongodb - 如何使用 docker-compose 为 mongo 数据库播种?

python - 在 Meteor 运行时,如何从另一个客户端访问 Meteor 的 MongoDB

mongodb - 如何在 MongoDB 中重命名集合?

mongodb - 将 MongoDB 集合的子集保存到另一个集合

mongodb - 服务器在 SASL 身份验证步骤 : Authentication failed

mongodb - meteor :如何备份我的 mongo 数据库

performance - MongoDB 'count()' 非常慢。我们如何改进/解决它?