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"
    }
  }
]);

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

相关推荐
✎ ﹏梦醒͜ღ҉繁华落℘9 小时前
单片机基础知识---stm32单片机的优先级
stm32·单片机·mongodb
麦聪聊数据10 小时前
数据服务化时代:企业数据能力输出的核心路径
数据库
shushangyun_10 小时前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
DARLING Zero two♡10 小时前
【MySQL数据库】数据类型与表约束
数据库·mysql
曹牧11 小时前
Oracle EXPLAIN PLAN
数据库·oracle
BD_Marathon11 小时前
SQL学习指南——视图
数据库·sql
活宝小娜11 小时前
mysql详细安装教程
数据库·mysql·adb
贤时间11 小时前
codex 助力oracle ebs 开发
数据库·oracle
JLWcai2025100912 小时前
铸造领域树脂砂轮|金利威多场景解决方案,20 + 配方覆盖全需求
mongodb·zookeeper·eureka·spark·rabbitmq·memcached·storm
秉承初心12 小时前
PostgreSQL 数据性能瓶颈突破实战
数据库·postgresql·oracle