简体   繁体   English

Mongodb多对多关系

[英]Mongodb Many to Many relationship

I am working on a webapp built on mean.js and part of the app is projects that have tags, similar to a blog site that has tags for each blog entry. 我正在开发一个基于mean.js的Web应用程序,该应用程序的一部分是带有标签的项目,类似于一个博客网站,每个博客条目都具有标签。

I have searched everywhere for a good example/tutorial explaining the best way to create a many to many relationship using mongodb/mongoose, but I can't seem to find anything. 我到处搜索了一个很好的示例/教程,解释了使用mongodb / mongoose创建多对多关系的最佳方法,但是我似乎找不到任何东西。

Each project can have multiple tags and I want the users to be able to find all projects with a specific tag. 每个项目可以有多个标签,我希望用户能够找到具有特定标签的所有项目。

I would really appreciate any suggestions/examples on the correct way to achieve this. 对于实现此目标的正确方法,我将不胜感激。 TIA TIA

Keep an array of id's in both collections. 在两个集合中都保留一个ID数组。 For example: 例如:

coll1: coll1:

{
  _id: ObjectId("56784ac717e12e59d600688a"),
  coll2_ids: [ObjectId("..."), ObjectId("..."), ObjectId("..."), ...]
}

coll2: coll2:

{
  _id: ObjectId("56784ac717e12e59d600677b"),
  coll1_ids: [ObjectId("..."), ObjectId("..."), ObjectId("..."), ...]
}

The advantage of this approach is that when you have a document from either collection, you can immediately pull all associated documents from the opposite collection simply by doing 这种方法的优点是,当您从两个集合中都有一个文档时,只需执行以下操作即可立即从相反的集合中提取所有关联的文档

obj = db.coll1.findOne({})
db.coll2.find({_id: {$in: obj['coll2_ids']}}) # returns all associated documents

and vice-versa. 反之亦然。

For many-to-many relationship, I have found mongoose-relationship really useful. 对于多对多关系,我发现猫鼬关系确实有用。

Here's a sample how you would solve it using mongoose-relationship module: 这是使用mongoose-relationship模块解决该问题的示例:

// In TagsModel.js file ----------------------------------------------
var mongoose = require('mongoose');
var schema = mongoose.Schema;
var relationship = require("mongoose-relationship");

var tagSchema = new schema({
   projects: [{
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Project',
      childPath: 'tags'
   }],
   ....
});

tagSchema.plugin(relationship, {
     relationshipPathName: 'projects'
});
var tagModel = mongoose.model('Tag', tagSchema);
--------------------------------------------------------------------

// In ProjectModel.js file -----------------------------------------
var mongoose = require('mongoose');
var schema = mongoose.Schema;

var Projects = new Schema({
    tags : [{
         type: mongoose.Schema.Types.ObjectId,
         ref: 'Tag'
         }],
    ...
});
var tagModel = mongoose.model('Project', tagSchema);

With this model structure, you will be able to find projects by specific tag and tags by project. 使用此模型结构,您将能够按特定标签和按项目的标签查找项目。

It seems you just want to have an Array of tags within your Project schema. 看来您只想在您的Project模式中包含一个标签数组。

var Tags = new Schema({
    name : String
});

var Projects = new Schema({
    tags : [String] // When you save a Tag just save the Name of it here
});

// This way you could easily look it up
// Assuming "name" was passed in as a param to express

Projects.find({ 'tags' : req.params.name }) // voila!

There are other ways as well (such as saving ObjectIds ). 还有其他方法(例如保存ObjectIds )。 This method would be easier if you think Tag names could change often but once again if a Tag is "deleted" you'd have to go look through all Projects to remove that ObjectId 如果您认为标记名称可能经常更改,则此方法会更容易,但是如果“标记”被删除,则必须再次查看所有项目以删除该ObjectId。

tags : [{ type: Schema.ObjectId, ref : 'Tags' }]

Basically the gist of this is that you are saving the a String or ObjectId reference (of the name) in an Array of "tags" within your Project model. 基本上,要点是将一个String或ObjectId引用(名称)保存在Project模型中的“标签”数组中。

Just remember when you're going to Edit Tag names / Delete a tag / etc, you'll want to go through and update (if they are saved as Strings) / remove those tags from any Projects that have it in their array. 只需记住,当您要编辑标签名称/删除标签/等时,您将要遍历和更新(如果它们另存为字符串)/从数组中包含标签的所有项目中删除这些标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM