Mongoose基于MongoDB建模并设置关联

一、模型关联

1、一对多/多对多

在一中关联多中的字段,type为mongoose.Schema.Types.ObjectId,并关联关联模型的名称。

const categorySchema = new mongoose.Schema({
  name: {type: String}
})

const tagSchema = new mongoose.Schema({
  name: {type: String}
})

const articleSchema = new mongoose.Schema({
  title: {type: String},
  content: {type: String},
  /* 一对多字段为一个对象 */
  category: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Category'   // 关联的模型
  },
  /* 多对多字段为一个数组 */
  tags: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Tag' 
  }]
})

2、关联模型的查询

(1)一对多查询

从关联对象中查询

const category = await Article.find().populate('category')
console.log(category)

结果为一个数组,数组中的对象含有category对象

[ { tags: [ 5ced005cf0b6b50fe429ffdb, 5ced005df0b6b50fe429ffdc ],
    _id: 5cecff6c9d129a1520c4fa9c,
    title: 'article1',
    content: 'article1',
    __v: 1,
    category: { _id: 5ced0007037e041ec0560c1a, name: 'node', __v: 0 } },
  { tags: [ 5ced005df0b6b50fe429ffdc ],
    _id: 5cecff6d9d129a1520c4fa9d,
    title: 'article2',
    content: 'article2',
    __v: 1,
    category: { _id: 5ced0008037e041ec0560c1b, name: 'vue', __v: 0 } } ]

(2)多对多查询

  const tag = await Article.find().populate('tags')
  console.log(tag)

结果为数组,被关联对象字段也是一个数组,包含多个对象

[ { tags: [ [Object], [Object] ],
    _id: 5cecff6c9d129a1520c4fa9c,
    title: 'article1',
    content: 'article1',
    __v: 1,
    category: 5ced0007037e041ec0560c1a },
  { tags: [ [Object] ],
    _id: 5cecff6d9d129a1520c4fa9d,
    title: 'article2',
    content: 'article2',
    __v: 1,
    category: 5ced0008037e041ec0560c1b } ]

3、从被关联模型中查询关联模型

(1)设置虚拟字段

在被关联模型中设置虚拟字段

categorySchema.virtual('articles', {  // 定义虚拟字段
  ref: 'Article',                                       // 关联的模型
  localField: '_id',                                // 内键,Category模型的id字段
  foreignField: 'category',                     // 外键,关联模型的category字段
  justOne: false                                        // 只查询一条数据
})

const tagSchema = new mongoose.Schema({
  name: {type: String}
})
tagSchema.virtual('articles', {
  ref: 'Article',
  localField: '_id',
  foreignField: 'tags',
  justOne: false
})

(2)查询

const article = await Tag.find().populate('articles')
console.log(article)

结果表面上看起来没有其他字段的信息,但虚拟字段的内容已经包括在内了;

[ { _id: 5ced005cf0b6b50fe429ffdb, name: 'tag1', __v: 0 },
  { _id: 5ced005df0b6b50fe429ffdc, name: 'tag2', __v: 0 } ]

继续在结果的数组中选择一个对象,并查询articles字段;

const article = await Tag.find().populate('articles')
console.log(article[0].articles)

结果就会为tag为tag1的所有文章。

[ { tags: [ 5ced005cf0b6b50fe429ffdb, 5ced005df0b6b50fe429ffdc ],
    _id: 5cecff6c9d129a1520c4fa9c,
    title: 'article1',
    content: 'article1',
    __v: 1,
    category: 5ced0007037e041ec0560c1a } ]
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • width: 65%;border: 1px solid #ddd;outline: 1300px solid #...
    邵胜奥阅读 10,378评论 0 1
  • REST API 可以让你用任何支持发送 HTTP 请求的设备来与 Parse 进行交互,你可以使用 REST A...
    Caroline嗯哼阅读 6,316评论 0 0
  • mongoose介绍及基本使用 标签(空格分隔): 未分类 两种和数据库交互的方式 使用原生语言查询 eg:sel...
    debt阅读 6,570评论 0 2
  • 初学Node.js接触到MongoDB数据库,阅读资料中推荐的都是Mongoose模块,可以更加方便的对数据库进行...
    LnEoi阅读 31,510评论 9 49
  • 11月22号,广州开始了中心区18块土地拍卖。“白云新城”某地盘的楼面价已高达46958元/平方米,而周边一手住宅...
    八豆说财阅读 4,097评论 0 0

友情链接更多精彩内容