mongodb多表查询,五个表查询

需求是这样的,而数据是从mysql导入进来的,由于mysql不支持数组类型的数据,所以有很多关联表。药剂里找药物,需要药剂与药物的关联表,然后再找药物表。从药物表里再找药物与成分关联表,最后再找成分表。

这里就涉及到了五张表的查询,之前的文章有讲解过多表查询这块。
mongodb以及mongoose插件如何多表查询,如同时查询店铺以及里面对应的商品_mongodb 多表查询-CSDN博客

这次我们来实战一下。

复制代码
db.fangji.aggregate([
{
            $match: {id:"2"},
        },
  {
    $lookup: {
      from: "fangji_herb",
      localField: "fangji_id",
      foreignField: "fangji_id",
      as: "fangjiHerb"
    }
  },
  {
    $lookup: {
      from: "herb_details",
      localField: "fangjiHerb.herb_id",
      foreignField: "herb_id",
      as: "herbs"
    }
  },
  {
    $lookup: {
      from: "offline_ingredient_herb",
      localField: "fangjiHerb.herb_id",
      foreignField: "herb_id",
      as: "offlineIngredientHerb"
    }
  },
  {
    $lookup: {
      from: "offline_ingredient",
      localField: "offlineIngredientHerb.item_id",
      foreignField: "item_id",
      as: "offlineIngredient"
    }
  }
]);

不过最终结果还是都放在不同的数组里了,需要前端慢慢处理结构,并转换为树的结构

相关推荐
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger1 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥2 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud2 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug2 天前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom2 天前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
麦兜*2 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud