使用文件clearTestData.php从数据库中的集合中删除数据。它包含一个查询,该查询似乎表明结尾处的集合为空。当我检查具有相同查询的list.php时,不会删除任何内容。完整的mongo noob在这里,服务器由unversity提供。通过浏览器“ server .... / clearTestData.php”进行访问进行测试。
ClearTestData.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());
?>
list.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的内容为:数组([0] => stdClass对象([_id] => MongoDB \ BSON \ ObjectId对象([oid] => 5bd1c6871c37212c6c218b42)[node] => N13 [data ] => 10)[1] => stdClass对象([_id] => MongoDB \ BSON \ ObjectId对象([oid] => 5bd1cad41c37212c66789d33)[节点] => N13 [数据] => 10)[2] => stdClass对象([_id] => MongoDB \ BSON \ ObjectId对象([oid] => 5bd1cb861c37212c66789d34)[节点] => N13 [数据] => 10)[3] => stdClass对象([_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)[节点] => N13 [数据] ] => 10)[6] => stdClass对象([_id] => MongoDB \ BSON \ ObjectId对象([oid] => 5bd722581c3721165402d2f3)[node] => N13 [data] => 10)[7] => stdClass对象([_ id] => MongoDB \ BSON \ ObjectId对象([oid] => 5be304c21c3721252357e882)[node] => N13 [data] => 10)[8] => stdClass对象([_id] => MongoDB \ BSON \ ObjectId对象([oid] => 5be308b71c372124f456ccb2)[node] => N13 [data] => 10)[9] => stdClass对象([_id] => MongoDB \ BSON \ ObjectId对象([oid] => 5be340901c372124f456ccb3)[node ] => N13 [数据] => 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))
在浏览器中清除输出:
成功连接到数据库。删除集合user.testData的内容。