thinkphp6使用MongoDB多个数据,聚合查询的坑

我使用的是thinkphp6,mongodb4.0实际业务查询

复制代码
  $list = Db::connect('tstd_mongo')
            ->table("$table_time.Item")
            ->where($where)
            ->order("Cause","asc")
            ->field('_id,DBID,Data,GSID,MainKind,ManID,DISTINCT(Serial) Serial,SubKind,Time')

            ->paginate($limit);

->table("$table_time.Item")

我是根据数据,要去不同的数据库查询,需要修改源码

topthink\think-orm\src\db\connector\Mogo.php function command()下 ,

复制代码
$dbName = $dbName ?: $this->dbName; 

修改成

复制代码
if (!empty($this->builder->dbName)){
    $dbName = $dbName ?: $this->builder->dbName;
    $this->dbName=$dbName;
}else{
    $dbName = $dbName ?: $this->dbName;
}

如果使用paginate count 就要修改

topthink\think-orm\src\db\builder\Mongo.php

复制代码
if (strstr($options['table'], '.')!== false) {
    $parts = explode('.',$options['table']);
    $count_db = !empty($parts[0])?$parts[0]:'';
    $count_table = !empty($parts[1])?$parts[1]:'';
    
    
    
    if (!empty($count_table)){
        $options['table']=$count_table;
    }
    if (!empty($count_db)){
        $this->dbName = $count_db;
    }
}

如果用 其他 ,aggregate,select 方法 也要加这段代码

相关推荐
码界奇点30 分钟前
基于Python的新浪微博数据爬虫系统设计与实现
数据库·爬虫·python·毕业设计·新浪微博·源代码管理
我科绝伦(Huanhuan Zhou)1 小时前
探索技术世界:我的GitHub数据库工具宝库
数据库·github
猫的玖月2 小时前
(一)MY SQL概述
数据库·sql
脑子进水养啥鱼?2 小时前
PostgreSQL .history 文件
数据库·postgresql
倔强的石头_3 小时前
5 个真实案例带你避坑:DolphinDB 实时写入、流订阅与高可用调优
数据库
虹科网络安全3 小时前
艾体宝新闻|Redis 月度更新速览:2026 年 3 月
数据库·redis·缓存
Nturmoils3 小时前
DolphinDB 实时时序数据处理踩坑实录:5 类生产故障排查与优化
数据库
csdn2015_3 小时前
postgresql 拼接字段
数据库
\xin3 小时前
pikachu自编SQL(POST)
java·数据库·sql
2301_815901973 小时前
SQL如何将多行记录聚合成逗号分隔字符串_GROUP_CONCAT技巧
jvm·数据库·python