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

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

问题描述

我正在为一个新项目设计数据库结构,我对 MongoDB 还很陌生,显然是 Mongoose.

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

我读过 Mongooses population 文档,其中有一对多的关系, 一个 Person 文档到多个 Story 文档,但让我困惑的部分是引用什么 Person 的 Story 文档而不是 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!

推荐答案

参考 population,这里从猫鼬中提取一个例子.

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天全站免登陆