33namespace MongoDB ;
44
55use MongoDB \Driver \Command ;
6+ use MongoDB \Driver \Cursor ;
67use MongoDB \Driver \Manager ;
78use MongoDB \Driver \Query ;
89use MongoDB \Driver \ReadPreference ;
9- use MongoDB \Driver \Result ;
1010use MongoDB \Driver \BulkWrite ;
1111use MongoDB \Driver \WriteConcern ;
1212
@@ -43,25 +43,24 @@ class Collection
4343
4444
4545 /**
46- * Constructs new Collection instance
46+ * Constructs new Collection instance.
4747 *
48- * This is the suggested CRUD interface when using phongo.
49- * It implements the MongoDB CRUD specification which is an interface all MongoDB
50- * supported drivers follow.
48+ * This class provides methods for collection-specific operations, such as
49+ * CRUD (i.e. create, read, update, and delete) and index management.
5150 *
52- * @param Manager $manager The phongo Manager instance
53- * @param string $ns Fully Qualified Namespace (dbname.collname )
54- * @param WriteConcern $wc The WriteConcern to apply to writes
55- * @param ReadPreference $rp The ReadPreferences to apply to reads
51+ * @param Manager $manager Manager instance from the driver
52+ * @param string $namespace Collection namespace (e.g. "db.collection" )
53+ * @param WriteConcern $writeConcern Default write concern to apply
54+ * @param ReadPreference $readPreference Default read preference to apply
5655 */
57- public function __construct (Manager $ manager , $ ns , WriteConcern $ wc = null , ReadPreference $ rp = null )
56+ public function __construct (Manager $ manager , $ namespace , WriteConcern $ writeConcern = null , ReadPreference $ readPreference = null )
5857 {
5958 $ this ->manager = $ manager ;
60- $ this ->ns = $ ns ;
61- $ this ->wc = $ wc ;
62- $ this ->rp = $ rp ;
59+ $ this ->ns = ( string ) $ namespace ;
60+ $ this ->wc = $ writeConcern ;
61+ $ this ->rp = $ readPreference ;
6362
64- list ($ this ->dbname , $ this ->collname ) = explode (". " , $ ns , 2 );
63+ list ($ this ->dbname , $ this ->collname ) = explode (". " , $ namespace , 2 );
6564 }
6665
6766 /**
@@ -87,14 +86,16 @@ public function aggregate(array $pipeline, array $options = array())
8786 "pipeline " => $ pipeline ,
8887 ) + $ options ;
8988
90- $ result = $ this ->_runCommand ($ this ->dbname , $ cmd );
91- $ doc = $ result -> toArray ();
89+ $ cursor = $ this ->_runCommand ($ this ->dbname , $ cmd );
90+
9291 if (isset ($ cmd ["cursor " ]) && $ cmd ["cursor " ]) {
93- return $ result ;
94- } else {
95- if ($ doc ["ok " ]) {
96- return new \ArrayIterator ($ doc ["result " ]);
97- }
92+ return $ cursor ;
93+ }
94+
95+ $ doc = current ($ cursor ->toArray ());
96+
97+ if ($ doc ["ok " ]) {
98+ return new \ArrayIterator ($ doc ["result " ]);
9899 }
99100
100101 throw $ this ->_generateCommandException ($ doc );
@@ -235,7 +236,7 @@ public function count(array $filter = array(), array $options = array())
235236 "query " => $ filter ,
236237 ) + $ options ;
237238
238- $ doc = $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray ();
239+ $ doc = current ( $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray () );
239240 if ($ doc ["ok " ]) {
240241 return $ doc ["n " ];
241242 }
@@ -325,7 +326,7 @@ public function distinct($fieldName, array $filter = array(), array $options = a
325326 "query " => $ filter ,
326327 ) + $ options ;
327328
328- $ doc = $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray ();
329+ $ doc = current ( $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray () );
329330 if ($ doc ["ok " ]) {
330331 return $ doc ["values " ];
331332 }
@@ -335,11 +336,15 @@ public function distinct($fieldName, array $filter = array(), array $options = a
335336 /**
336337 * Drop this collection.
337338 *
338- * @return Result
339+ * @see http://docs.mongodb.org/manual/reference/command/drop/
340+ * @return Cursor
339341 */
340342 public function drop ()
341343 {
342- // TODO
344+ $ command = new Command (array ('drop ' => $ this ->collname ));
345+ $ readPreference = new ReadPreference (ReadPreference::RP_PRIMARY );
346+
347+ return $ this ->manager ->executeCommand ($ this ->dbname , $ command , $ readPreference );
343348 }
344349
345350 /**
@@ -348,7 +353,7 @@ public function drop()
348353 * @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
349354 * @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndex/
350355 * @param string $indexName
351- * @return Result
356+ * @return Cursor
352357 * @throws InvalidArgumentException if "*" is specified
353358 */
354359 public function dropIndex ($ indexName )
@@ -361,7 +366,7 @@ public function dropIndex($indexName)
361366 *
362367 * @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
363368 * @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndexes/
364- * @return Result
369+ * @return Cursor
365370 */
366371 public function dropIndexes ()
367372 {
@@ -376,7 +381,7 @@ public function dropIndexes()
376381 *
377382 * @param array $filter The find query to execute
378383 * @param array $options Additional options
379- * @return Result
384+ * @return Cursor
380385 */
381386 public function find (array $ filter = array (), array $ options = array ())
382387 {
@@ -434,7 +439,7 @@ public function findOneAndDelete(array $filter, array $options = array())
434439 "query " => $ filter ,
435440 ) + $ options ;
436441
437- $ doc = $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray ();
442+ $ doc = current ( $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray () );
438443 if ($ doc ["ok " ]) {
439444 return $ doc ["value " ];
440445 }
@@ -471,7 +476,7 @@ public function findOneAndReplace(array $filter, array $replacement, array $opti
471476 "query " => $ filter ,
472477 ) + $ options ;
473478
474- $ doc = $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray ();
479+ $ doc = current ( $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray () );
475480 if ($ doc ["ok " ]) {
476481 return $ doc ["value " ];
477482 }
@@ -509,7 +514,7 @@ public function findOneAndUpdate(array $filter, array $update, array $options =
509514 "query " => $ filter ,
510515 ) + $ options ;
511516
512- $ doc = $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray ();
517+ $ doc = current ( $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray () );
513518 if ($ doc ["ok " ]) {
514519 return $ doc ["value " ];
515520 }
@@ -948,7 +953,7 @@ public function insertOne(array $document)
948953 *
949954 * @see http://docs.mongodb.org/manual/reference/command/listIndexes/
950955 * @see http://docs.mongodb.org/manual/reference/method/db.collection.getIndexes/
951- * @return Result
956+ * @return Cursor
952957 */
953958 public function listIndexes ()
954959 {
0 commit comments