首页 > 解决方案 > 用 php 清除 mongoDB 集合,似乎不能永久工作

问题描述

使用文件 clearTestData.php 从我的数据库中的集合中删除数据。它包括一个查询,该查询似乎显示该集合最后是空的。但是,当我检查具有相同查询的 list.php 时,没有任何内容被删除。在这里完成 mongo noob,服务器由 unviersity 提供。通过浏览器访问“server..../clearTestData.php”来测试两者。

清除测试数据.php:

<?php
   // connect to mongodb
   $user = "muntean";
   require '/var/composer/vendor/autoload.php';
   $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
   echo "Connection to database successfully. ";
   $collection = new MongoDB\Collection($manager, $user, 'testData');
   echo "deleting contents of collection user.testData. ";

   // now remove the document
   $collection->remove(array("node"=>"xyzWXX"),false);
   // db.testData.deleteMany({});
   echo "Documents deleted successfully.";

   $filter = [];
   $options = [];
   $query = new MongoDB\Driver\Query($filter, $options);
   $cursor = $manager->executeQuery("$user.testData", $query);
   print("The contents of the collection $user.testData are: ");
   print_r($cursor->toArray());
?>

列表.php:

<html><body>
<?php
   $user = "muntean";
   require '/var/composer/vendor/autoload.php';
   $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
   $collection = new MongoDB\Collection($manager, $user, 'testData');
   $filter = [];
   $options = [];
   $query = new MongoDB\Driver\Query($filter, $options);
   $cursor = $manager->executeQuery("$user.testData", $query);
   print("The contents of the collection $user.testData are:");
   print_r($cursor->toArray());
?>

浏览器中的输出列表:

集合 muntean.testData 的内容为:Array ( [0] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5bd1c6871c37212c6c218b42 ) [node] => N13 [data] => 10 ) [1] => stdClass 对象 ( [_id] => MongoDB\BSON\ObjectId 对象 ( [oid] => 5bd1cad41c37212c66789d33 ) [node] => N13 [data] => 10 ) [2] => stdClass 对象 ( [_id] => MongoDB\BSON\ObjectId Object ([oid] => 5bd1cb861c37212c66789d34) [node] => N13 [data] => 10) [3] => stdClass Object ([_id] => MongoDB\BSON\ObjectId对象([oid] => 5bd718621c3721165402d2f2)[节点] => N13 [数据] => 10)[4] => stdClass 对象([_id] => MongoDB\BSON\ObjectId 对象([oid] => 5bd7221f1c372116132eaf82)[节点] => N13 [数据] => 10 ) [5] => stdClass 对象 ( [_id] =>MongoDB\BSON\ObjectId 对象 ( [oid] => 5bd722241c372116125a1fc2 ) [node] => N13 [data] => 10 ) [6] => stdClass 对象 ( [_id] => MongoDB\BSON\ObjectId 对象 ( [oid] => 5bd722581c3721165402d2f3 ) [节点] => N13 [数据] => 10 ) [7] => stdClass 对象 ( [_id] => MongoDB\BSON\ObjectId 对象 ( [oid] => 5be304c21c3721252357e882 ) [节点] => N13 [数据] => 10 ) [8] => stdClass 对象 ( [_id] => MongoDB\BSON\ObjectId 对象 ( [oid] => 5be308b71c372124f456ccb2 ) [节点] => N13 [数据] => 10 ) [9] => stdClass 对象 ( [_id] => MongoDB\BSON\ObjectId 对象 ( [oid] => 5be340901c372124f456ccb3 ) [node] => N13 [data] => 10 ) [10] => stdClass 对象 ( [_id] => MongoDB\BSON\ObjectId 对象 ( [oid] => 5be373421c372124f10feee2 ) [节点] => N13 [数据] => 10 ) [11] =>stdClass 对象 ( [_id] => MongoDB\BSON\ObjectId 对象 ( [oid] => 5c6d6a651c372113771565f2 ) [node] => N13 [data] => 10 ) [12] => stdClass 对象 ( [_id] => MongoDB\ BSON\ObjectId 对象 ( [oid] => 5c6d6a8a1c3721137b0653b2 ) [节点] => N13 [数据] => 10 ) [13] => stdClass 对象 ( [_id] => MongoDB\BSON\ObjectId 对象 ( [oid] => 5c6d6af51c372113791c77c2 ) [节点] => N13 [数据] => 53.374057769775-6.6027770042419 ) )N13 [数据] => 53.374057769775-6.6027770042419 ) )N13 [数据] => 53.374057769775-6.6027770042419 ) )

在浏览器中清除输出:

成功连接到数据库。删除集合 user.testData 的内容。

标签: phpmongodb

解决方案


推荐阅读