猫鼬–如何限制populate()级别的深度? [英] Mongoose – how to limit depth of populate() level?

查看:56
本文介绍了猫鼬–如何限制populate()级别的深度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种模式:

var categorySchema = mongoose.Schema({
    title: String,
    blogs: [{ type: ObjectId, ref: 'Blog' }]
})

var blogSchema = mongoose.Schema({
    title: String,
    description: String,
    category: [{ type: ObjectId, ref: 'Category' }],
    created: {type: Number, default: new Date().getTime()}
})

var Category = mongoose.model('Category', categorySchema)
var Blog = mongoose.model('Blog', blogSchema)

他们在交叉引用:

  1. 博客对象包含Category个对象(引用)的数组,以便能够获取与此博客相关的所有类别.
  2. 类别对象包含一个Blog对象(引用)数组,以获取该类别的所有博客.
  1. Blog object contains an array of Category objects (refs) to be able to get all the categories this blog is related to.
  2. Category object contains an array of Blog objects (refs) to be able to get all the blogs of this category.

问题是当我尝试获取某个Blog时.我需要填充类别数组以获取其标题:

The problem is when I try to get a certain Blog. I need to populate category array to get it's titles:

Blog
    .findOne({_id: _._id})
    .populate('category')
    .exec(function (err, __) {
        callback(err, __);
    })

我知道...

{ title: 'My Blog',
  description: 'description',,
  _id: 51cb6bd845ba145e02000001,
  __v: 0,
  created: 1372285906662,
  category: 
   [ { __v: 0,
       _id: 51cb5ed6fd63867905000002,
       priority: 3,
       title: 'Music',
       blogs: [Object] } ],
}

是的,我得到了类别标题,但是我在博客中也得到了一些对象-它们也已被填充.但是Blog对象可能包含许多子对象(帖子),并且我们记得category字段.那么,category字段中的所有对象将被递归填充,并且由于我在Blog和Category之间有交叉链接,它将以循环填充?

Yep, I get categories titles, but I also get some objects in blogs – they were also populated. But Blog-objects may contain a lot of subobjects (posts) and as we remember category field. So, all the objects in category field will be populated recursively and as I have cross links between Blog and Category it will be populating in a loop?

如何限制人口水平?我不希望填充Blog/category [n]/blogs: [],仅填充直接category字段,例如title.谢谢.

How to limit population level? I dont want Blog/category[n]/blogs: [] to be populated, only direct category fields, such as title. Thanks.

推荐答案

来自猫鼬文档 :

有争议的是我们真的想要两组指针,因为它们可能 失去同步.相反,我们可以跳过填充并直接找到() 我们感兴趣的故事.

It is debatable that we really want two sets of pointers as they may get out of sync. Instead we could skip populating and directly find() the stories we are interested in.

我会在类别架构中删除博客的引用,而只是查询您感兴趣的文档.

I would remove the reference of blogs in the category schema and just query for the documents that you are interested.

博客对象包含一个类别对象(引用)数组,以便能够获取与该博客相关的所有类别":

"Blog object contains an array of Category objects (refs) to be able to get all the categories this blog is related to":

Blog.findOne({_id: blogId})
    .populate('category')
    .exec(function (err, blog) {
        callback(err, blog);
        //blog.category is the array you want
    })

类别对象包含一系列Blog对象(引用),以便能够获取该类别的所有博客":

"Category object contains an array of Blog objects (refs) to be able to get all the blogs of this category":

Blog.find({category: catId}) 
    .exec(function (err, blogs) {
        callback(err, blogs);                        
    })

这是 $ all查询实际上是在类别数组中搜索指定的ID.

This is a simplified syntax of the $all query that actually searches inside the array of categories for the specified id.

这篇关于猫鼬–如何限制populate()级别的深度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆