具有一对多关系的猫鼬文档参考 [英] Mongoose document references with a one-to-many relationship

查看:76
本文介绍了具有一对多关系的猫鼬文档参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个新项目设计数据库结构,对MongoDB以及Mongoose来说,我还很陌生.

I'm working on designing a database structure for a new project, and I'm pretty new to MongoDB, and obviously Mongoose.

我已阅读过猫鼬人口文档,该文档具有一对多关系,其中一个Person文档到许多Story文档,但是让我感到困惑的是,在Person模式中设置了它,而不是在Story文档中引用了它所属的Person文档. Story文档拥有"的数组.

I've read Mongooses population documentation, where it has a one-to-many relationship, with one Person document to many Story documents, but the part that confuses me is where instead of the Story documents referencing what Person document it belongs to, the Person schema has it setup so it has an array of what Story documents it 'owns'.

我正在设置与此非常相似的内容.但是我一直认为,创建新的Story文档具有Person文档ID会更容易.但这可能只是因为我对使用联接的MySQL关系更加熟悉.

I'm setting up something very similar to this. But I keep thinking it would be easier when creating new Story documents to have the Person document ID. But maybe thats just because I'm more familiar with MySQL relationships using joins.

如果这是执行此操作的最佳方法(并且我敢肯定,因为它存在于文档中),那么在创建新的Story文档时,更新相关故事中的数组的最佳方法是什么? People它属于哪个文件?我看过但找不到更新现有文档以添加对其他文档的引用(或为此删除它们)的任何示例

If this is the best way to do it (and I'm sure it is, since its in the docs), when new Story documents are created, whats the best way to update the array of stories in the associated People document it belongs to? I looked but couldn't find any examples of updating existing documents to add references to other documents (or deleting them for that matter)

我敢肯定,这是一个简单的解决方案,但我只是忽略了什么,但是任何帮助都会很棒.谢谢!

I'm sure this is an easy solution that I just overlooked or something, but any help would be great. Thanks!

推荐答案

请参阅人口,这里从猫鼬中提取一个例子.

Refer to population, here extract an example from Mongoose.

var mongoose = require('mongoose')
, Schema = mongoose.Schema

var personSchema = Schema({
  _id     : Schema.Types.ObjectId,
  name    : String,
  age     : Number,
  stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});

var storySchema = Schema({
  _creator : { type: Schema.Types.ObjectId, ref: 'Person' },
  title    : String,
  fans     : [{ type: Schema.Types.ObjectId, ref: 'Person' }]
});

var Story  = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);

因此,关于Story模型的示例将相关的Person._id存储在Story._creator中.找到Story的文档时,可以使用populate()方法来定义要同时检索的Person模型中的哪个属性,例如:

So the example about, Story model stores related Person._id in Story._creator. When you find a document of Story, you can use populate() method to define which attribute in Person model you want to retrieve at the same time, such as:

Story.findOne({_id: 'xxxxxxx'}).populate('person', 'name age').exec(function(err, story) {
  console.log('Story title: ', story.title);
  console.log('Story creator', story.person.name);
});

我相信这就是您要寻找的.否则,您可以使用嵌套的收藏集代替.

I believe this is what you looking for. Or else, you can use nested collections instead.

这篇关于具有一对多关系的猫鼬文档参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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