douzao9845 2019-02-27 03:05 采纳率: 0%
浏览 181

使用php清除mongoDB集合,似乎永久不起作用

Using a file clearTestData.php to remove data from a collection in my database. It includes a query that seems to be showing the collection is empty at the end. When I check with list.php however which has the same query, nothing is deleted. Complete mongo noob here, server provided by unviersity. Testing both by accessing through browser "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());
?>

Output list in browser:

The contents of the collection muntean.testData are:Array ( [0] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5bd1c6871c37212c6c218b42 ) [node] => N13 [data] => 10 ) [1] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5bd1cad41c37212c66789d33 ) [node] => N13 [data] => 10 ) [2] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5bd1cb861c37212c66789d34 ) [node] => N13 [data] => 10 ) [3] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5bd718621c3721165402d2f2 ) [node] => N13 [data] => 10 ) [4] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5bd7221f1c372116132eaf82 ) [node] => N13 [data] => 10 ) [5] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5bd722241c372116125a1fc2 ) [node] => N13 [data] => 10 ) [6] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5bd722581c3721165402d2f3 ) [node] => N13 [data] => 10 ) [7] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5be304c21c3721252357e882 ) [node] => N13 [data] => 10 ) [8] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5be308b71c372124f456ccb2 ) [node] => N13 [data] => 10 ) [9] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5be340901c372124f456ccb3 ) [node] => N13 [data] => 10 ) [10] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5be373421c372124f10feee2 ) [node] => N13 [data] => 10 ) [11] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5c6d6a651c372113771565f2 ) [node] => N13 [data] => 10 ) [12] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5c6d6a8a1c3721137b0653b2 ) [node] => N13 [data] => 10 ) [13] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5c6d6af51c372113791c77c2 ) [node] => N13 [data] => 53.374057769775-6.6027770042419 ) )

output clear in browser:

Connection to database successfully. deleting contents of collection user.testData.

展开全部

  • 写回答

0条回答 默认 最新

      编辑
      预览

      报告相同问题?

      手机看
      程序员都在用的中文IT技术交流社区

      程序员都在用的中文IT技术交流社区

      专业的中文 IT 技术社区,与千万技术人共成长

      专业的中文 IT 技术社区,与千万技术人共成长

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      客服 返回
      顶部