猫鼬 - 将对象彼此链接而不重复

时间:2021-10-18 08:36:10

I have a model "Category". Collection categories contains several objects. I also a have model "Post". Collection posts may contain a lot of objects with users' posts. "Post" object may relate to 1+ categories. How to link "Post" object to 1+ "Category"-objects without placing "Post"-object inside "Category"-object as subdocument? Certainly, I need to have an option to find all posts related to certain category.

我有一个模型“类别”。集合类别包含多个对象。我也有一个模型“发布”。收集帖子可能包含很多带有用户帖子的对象。 “发布”对象可能涉及1个以上的类别。如何将“发布”对象链接到1+“类别” - 对象而不将“发布” - 对象放在“类别” - 对象中作为子文档?当然,我需要有一个选项来查找与某个类别相关的所有帖子。

One of the ways I can imagine is to store in "Post"-object obj_id of all categories which it's related to. Smth like this:

我可以想象的方法之一是存储在与其相关的所有类别的“Post”-object obj_id中。 Smth是这样的:

var postSchema = mongoose.Schema({
  title: String,
  description: String,
  category: [ObjectId],
  created_time: Number,
})

and add category later...

并在以后添加类别...

post.category.push(obj_id);

but is it really a mongoose-way? Which way is correct? Thanks.

但它真的是一种猫鼬的方式吗?哪种方式正确?谢谢。

P.S. I've also read about population methods in mongoose docs, may it be useful in my case? Still not completely clear for me what is this.

附:我也读过mongoose docs中的种群方法,在我的情况下是否有用?对我来说还不完全清楚这是什么。

1 个解决方案

#1


18  

Populate is a better tool for this since you are creating a many to many relationship between posts and categories. Subdocuments are appropriate when they belong exclusively to the parent object. You will need to change your postSchema to use a reference:

Populate是一个更好的工具,因为你在帖子和类别之间创建了多对多的关系。当子文档专属于父对象时,它们是合适的。您需要更改postSchema才能使用参考:

var postSchema = mongoose.Schema({
  title: String,
  description: String,
  category: [{ type: Schema.Types.ObjectId, ref: 'Category' }],
  created_time: Number,
});

You can add categories by pushing documents onto the array:

您可以通过将文档推送到阵列来添加类别:

post.category.push(category1);
post.save(callback);

Then rehydrate them during query using populate:

然后使用populate在查询期间重新水化它们:

Post.findOne({ title: 'Test' })
.populate('category') 
.exec(function (err, post) {
   if (err) return handleError(err);
   console.log(post.category); 
});

#1


18  

Populate is a better tool for this since you are creating a many to many relationship between posts and categories. Subdocuments are appropriate when they belong exclusively to the parent object. You will need to change your postSchema to use a reference:

Populate是一个更好的工具,因为你在帖子和类别之间创建了多对多的关系。当子文档专属于父对象时,它们是合适的。您需要更改postSchema才能使用参考:

var postSchema = mongoose.Schema({
  title: String,
  description: String,
  category: [{ type: Schema.Types.ObjectId, ref: 'Category' }],
  created_time: Number,
});

You can add categories by pushing documents onto the array:

您可以通过将文档推送到阵列来添加类别:

post.category.push(category1);
post.save(callback);

Then rehydrate them during query using populate:

然后使用populate在查询期间重新水化它们:

Post.findOne({ title: 'Test' })
.populate('category') 
.exec(function (err, post) {
   if (err) return handleError(err);
   console.log(post.category); 
});