php给mongodb添加索引

php给mongodb添加索引,第1张

概述<?php/***mongo字段加索引*CreatedbyPhpStorm.*User:Administrator*Date:2021/6/11*Time:15:02*/$mongoDB='deyibao';//mongo数据库名$mongoClient=newMongoClient();//mongo连接$db=$mongoClient->selectDB($mongoDB);//选择数据库$colle
<?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]);        }    }}
根据自己需求更改,此文章只为记录 总结

以上是内存溢出为你收集整理的php给mongodb添加索引全部内容,希望文章能够帮你解决php给mongodb添加索引所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1166682.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存