猫鼬模式中的引用 [英] Reference in mongoose schema

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

问题描述

我有两个不同的架构,我将其放在两个单独的文件中,但是问题是如何引用架构对象ID?从一个文件到另一个文件.

I have two different schemas and i put it in two separate files, but the question is how do I reference the Schema object Id? from one file to another.

这基本上是单个文件中的两个架构

This is basically, two schema in a single file

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

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

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

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

两个不同文件中的两个模式

Two Schemas in two different files

1)person.js

1) person.js

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

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

module.exports = mongoose.model('Person', personSchema);

2)story.js

2) story.js

 var storySchema = Schema({
  _creator : { type: Number, ref: '?' },
   title    : String,
   fans     : [{ type: Number, ref: '?' }]
});


module.exports = mongoose.model('Story', storySchema);

我应该在两个架构文件的ref中放入什么?因为它们都在单独的文件中.

What should I put in the ref in both schema file? since both of them are in a separate file.

推荐答案

ref属性只是字符串,它们在单独的文件中并不重要.

The ref properties are just strings, it does not matter that they are in separate files.

尽管它们是不同的文件,但仍然执行以下操作仍然可以:

Doing the following despite them in being different files will still work:

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

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

这篇关于猫鼬模式中的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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