hello,word

fastadmin mongodb 处理及使用

1.需要安装mongodb,使用的宝塔,在软件商城中安装

2.安装mongodb的扩展,使用的宝塔\phpstudy环境,在需要的网站->php环境配置中安装monogdb的扩展

3.使用composer安装mongodb/mongodb库

composer require mongodb/mongodb

      3.1)如果你安装php的mongodb扩展,使用

        use MongoDB\Client;

      new MongoDB\Client();

      报错     致命错误: Class 'MongoDB\Client' not found   大概率是没有进行步骤3

     如果安装了MongoDB的扩展,那么可以使用\MongoDB\Driver\Manager类作为底层驱动操作

4.常用方法,后续增加,此处不全

// MongoDB 连接(替换成你的)

$mongo = new MongoClient("mongodb://username:password@localhost:27017");// 或无认证 mongodb://localhost:27017

$db = $mongo->selectDatabase('your_database');// 数据库名

$collection = $db->selectCollection('charging_stations'); // 集合名

$metaCollection = $db->selectCollection('charging_meta'); // 存储元数据(上次哈希、更新时间)

$lastMeta = $metaCollection->findOne(['type' => 'evroam_hash']);// 获取上次哈希

$lastHash = $lastMeta['hash'] ?? '';

$collection->deleteMany([]); // 清空旧数据

$collection->insertMany($batch);// 批量插入(提高性能)

/ 更新元数据哈希和时间

$metaCollection->updateOne(

  ['type' => 'evroam_hash'],  //where条件

  ['$set' =>[ 'hash' => $newHash, 'updated_at' => new UTCDateTime(time() * 1000), 'total_sites' => count($allData) ]],//更新内容

        ['upsert' => true]

);

 

 

 

posted @ 2026-01-30 10:03  tying  阅读(2)  评论(0)    收藏  举报