ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

mongodb设置数据关联

2021-02-24 20:35:19  阅读:163  来源: 互联网

标签:__ name parent mongodb 关联 设置 id categories


一、模型关联

来自: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 }]

标签:__,name,parent,mongodb,关联,设置,id,categories
来源: https://www.cnblogs.com/setbug/p/14443630.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有