zoukankan      html  css  js  c++  java
  • Mongoose基于MongoDB建模并设置关联

    一、模型关联

    1.一对多/多对多

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

     1 const Categoryschema = new mongoose.Schema({
     2   name: { type: String },
     3   parent: {
     4     type: mongoose.SchemaTypes.ObjectId,
     5     ref: 'Category'
     6   }
     7 })
     8 
     9 const Articleschema = new mongoose.Schema({
    10   title: { type: String },
    11   body: { type: String },
    12   categories: [{
    13     type: mongoose.SchemaTypes.ObjectId,
    14     ref: 'Category'
    15   }]
    16 }, {
    17   timestamps: true  // 时间戳
    18 })

     2.关联模型的查询

    (1)一对多查询

    从关联对象中查询

    const categories = await Category.find().populate('parent')
    console.log(categories)

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

     1 [{
     2   __id: 5cecff6c9d129a1520c4fa9c,
     3   name: '公告',
     4   parent: {
     5     __id: 5ced0007037e041ec0560c1a,
     6     name: '新闻资讯'
     7   }
     8 }, {
     9   __id: 5cecff6d9d129a1520c4fa9d,
    10   name: '活动',
    11   parent: {
    12     __id: 5ced0008037e041ec0560c1b,
    13     name: '新闻资讯'
    14   }
    15 }]

    (2)多对多查询

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

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

     1 [{
     2   __id: 5cecff6c9d129a1520c4fa9c,
     3   title: 'title1',
     4   body: 'body1',
     5   categories: [ [Object], [Object] ]
     6 }, {
     7   __id: 5cecff6d9d129a1520c4fa9d,
     8   title: 'title2',
     9   body: 'body2',
    10   categories: [ [Object], [Object] ]
    11 }]

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

    (1)设置虚拟字段

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

     1 categorySchema.virtual('children', {  // 定义虚拟字段
     2   ref: 'Category',                               // 关联的模型
     3   localField: '_id',                          // 内键,Category模型的id字段
     4   foreignField: 'parent',               // 外键,关联模型的category字段
     5   justOne: false                            // 只查询一条数据
     6 })
     7  8 categorySchema.virtual('newsList', {
     9   ref: 'Article',
    10   localField: '_id',
    11   foreignField: 'categories',
    12   justOne: false
    13 })

    (2)查询

    const article = await Category.find({ name: '新闻资讯' }).populate({
    path: 'articles',
    populate: { path: 'newslist' }
    }).lean() console.log(article)

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

     1 [{
     2   __id: 5cd3f5cf0dce1d58335b2c3f,
     3   name: '新闻资讯',
     4   children: [{
     5     __id: 5ced005cf0b6b50fe429ffdb,
     6     parent: 5cd3f5cf0dce1d58335b2c3f,
     7     name: '公告',
     8     newsList: [{
     9       __id: 5cecff6d9d129a1520c4fa9d,
    10       categories: []
    11     }]
    12   }, {
    13     ...
    14   }]
    15 }, {
    16   ...
    17 }]
  • 相关阅读:
    python中不可变数据类型和可变数据类型
    你分得清Python中:“索引和切片”吗?
    Python Django中一些少用却很实用的orm查询方法
    jQuery on()方法
    jquery.flexslider-min.js实现banner轮播图效果
    jQuery 树型菜单插件(Treeview)
    jQuery Growl 插件(消息提醒)
    jQuery Autocomplete 用户快速找到并从预设值列表中选择
    jQuery Accordion 插件用于创建折叠菜单
    jquery.validate.js 验证框架详解
  • 原文地址:https://www.cnblogs.com/galaxy2490781718/p/13374749.html
Copyright © 2011-2022 走看看