• mongodb设置数据关联


    一、模型关联

    来自:https://www.cnblogs.com/galaxy2490781718/p/13374749.html

    1.一对多/多对多

    在一中关联多中的字段,type为 mongoose.Schema.Types.ObjectId ,并关联关联模型的名称。ObjectId根据数据类型可换成String

    复制代码
     1 const Categoryschema = new mongoose.Schema({
     2   name: { type: String },
     3   parent: {
     4     type: mongoose.Schema.Types.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.Schema.Types.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 }]
  • 相关阅读:
    vb学习笔记
    spfa模版
    spfa slf优化
    接口总结
    SAP屏幕穿透
    判断可编辑字段,用户输入的数据是否为纯数字(包含带小数点的小数)
    对于ALV中的可编辑字段,当输入的数据不满足某种条件时,我们需要将它恢复到修改前的数据,并刷新ALV。但是可编辑的字段刷新后仍然时修改后的数据,此处记录一种方法。
    ALV中可编辑字段数据变化时,对变化的数据进行操作。
    通过UPDATE 往数据库中更新数据
    SE16N 中设置为可编辑状态
  • 原文地址:https://www.cnblogs.com/setbug/p/14443630.html
Copyright © 2020-2023  润新知