zoukankan      html  css  js  c++  java
  • php给mongodb添加索引

    <?php
    /**
     * mongo字段加索引
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2021/6/11
     * Time: 15:02
     */
    
    $mongoDB = 'deyibao';//mongo数据库名
    $mongoClient = new MongoClient();//mongo连接
    $db = $mongoClient->selectDB($mongoDB);//选择数据库
    $collectionNames = $db->getCollectionNames();//获取所有的集合名
    $rule1 = '/task_record_.*/';//正则表达式
    $rule2 = '/turn_.+_record_.*/';
    foreach ($collectionNames as $k=>$v){
        $index = $db->$v->getIndexInfo();//获取索引
        if (preg_match_all($rule1,$v)){
            foreach ($index as $key=>$val){
                if (!strstr('date',$val['name'])) $collection = $db->$v->ensureIndex(['date' => 1],['background'=>true]);
                if (!strstr('userId',$val['name'])) $collection = $db->$v->ensureIndex(['userId' => 1],['background'=>true]);
            }
        }
        if (preg_match_all($rule2,$v) || strstr($v,'turn_luck_money')){
            foreach ($index as $key=>$val){
                if (!strstr('date',$val['name'])) $collection = $db->$v->ensureIndex(['date' => 1],['background'=>true]);
                if (!strstr('userId',$val['name'])) $collection = $db->$v->ensureIndex(['userId' => 1],['background'=>true]);
                if (!strstr('type',$val['name'])) $collection = $db->$v->ensureIndex(['type' => 1],['background'=>true]);
                if (!strstr('createTime',$val['name'])) $collection = $db->$v->ensureIndex(['createTime' => 1],['background'=>true]);
            }
        }
        if (strstr($v,'turn_user_sign') || strstr($v,'turn_message') || strstr($v,'turn_group_ranklist_log')) {
            foreach ($index as $key=>$val){
                if (!strstr('userId',$val['name'])) $collection = $db->$v->ensureIndex(['userId' => 1],['background'=>true]);
            }
        }
        if (strstr($v,'turn_group_ranklist')) {
            foreach ($index as $key=>$val){
                if (!strstr('date',$val['name'])) $collection = $db->$v->ensureIndex(['date' => 1],['background'=>true]);
                if (!strstr('groupId',$val['name'])) $collection = $db->$v->ensureIndex(['groupId' => 1],['background'=>true]);
            }
        }
        if (strstr($v,'turn_contribution')) {
            foreach ($index as $key=>$val){
                if (!strstr('userId',$val['name'])) $collection = $db->$v->ensureIndex(['userId' => 1],['background'=>true]);
                if (!strstr('ctbUserId',$val['name'])) $collection = $db->$v->ensureIndex(['ctbUserId' => 1],['background'=>true]);
                if (!strstr('date',$val['name'])) $collection = $db->$v->ensureIndex(['date' => 1],['background'=>true]);
                if (!strstr('tierNum',$val['name'])) $collection = $db->$v->ensureIndex(['tierNum' => 1],['background'=>true]);
            }
        }
        if (strstr($v,'turn_exchange')) {
            foreach ($index as $key=>$val){
                if (!strstr('date',$val['name'])) $collection = $db->$v->ensureIndex(['date' => 1],['background'=>true]);
                if (!strstr('configId',$val['name'])) $collection = $db->$v->ensureIndex(['configId' => 1],['background'=>true]);
                if (!strstr('userId',$val['name'])) $collection = $db->$v->ensureIndex(['userId' => 1],['background'=>true]);
                if (!strstr('type',$val['name'])) $collection = $db->$v->ensureIndex(['type' => 1],['background'=>true]);
            }
        }
    }
    

    根据自己需求更改,此文章只为记录

  • 相关阅读:
    MongoDB 学习笔记(七):主从复制与副本集
    MongoDB 学习笔记(六):备份与用户管理
    MongoDB 学习笔记(五):固定集合、GridFS文件系统与服务器端脚本
    MongoDB 学习笔记(四):索引
    MongoDB 学习笔记(三):分页、排序与游标
    MongoDB 学习笔记(一):安装及简单shell操作
    MongoDB 学习笔记(二):shell中执行增删查改
    mongoDB 入门指南、示例
    mongoDB 介绍(特点、优点、原理)
    企业级任务调度框架Quartz(7) 线程在Quartz里的意义(1)
  • 原文地址:https://www.cnblogs.com/ljkltt/p/14876084.html
Copyright © 2011-2022 走看看