3
3
namespace MongoDB ;
4
4
5
5
use MongoDB \Driver \Command ;
6
+ use MongoDB \Driver \Cursor ;
6
7
use MongoDB \Driver \Manager ;
7
8
use MongoDB \Driver \Query ;
8
9
use MongoDB \Driver \ReadPreference ;
9
- use MongoDB \Driver \Result ;
10
10
use MongoDB \Driver \BulkWrite ;
11
11
use MongoDB \Driver \WriteConcern ;
12
12
@@ -43,25 +43,24 @@ class Collection
43
43
44
44
45
45
/**
46
- * Constructs new Collection instance
46
+ * Constructs new Collection instance.
47
47
*
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.
51
50
*
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
56
55
*/
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 )
58
57
{
59
58
$ 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 ;
63
62
64
- list ($ this ->dbname , $ this ->collname ) = explode (". " , $ ns , 2 );
63
+ list ($ this ->dbname , $ this ->collname ) = explode (". " , $ namespace , 2 );
65
64
}
66
65
67
66
/**
@@ -87,14 +86,16 @@ public function aggregate(array $pipeline, array $options = array())
87
86
"pipeline " => $ pipeline ,
88
87
) + $ options ;
89
88
90
- $ result = $ this ->_runCommand ($ this ->dbname , $ cmd );
91
- $ doc = $ result -> toArray ();
89
+ $ cursor = $ this ->_runCommand ($ this ->dbname , $ cmd );
90
+
92
91
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 " ]);
98
99
}
99
100
100
101
throw $ this ->_generateCommandException ($ doc );
@@ -235,7 +236,7 @@ public function count(array $filter = array(), array $options = array())
235
236
"query " => $ filter ,
236
237
) + $ options ;
237
238
238
- $ doc = $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray ();
239
+ $ doc = current ( $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray () );
239
240
if ($ doc ["ok " ]) {
240
241
return $ doc ["n " ];
241
242
}
@@ -325,7 +326,7 @@ public function distinct($fieldName, array $filter = array(), array $options = a
325
326
"query " => $ filter ,
326
327
) + $ options ;
327
328
328
- $ doc = $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray ();
329
+ $ doc = current ( $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray () );
329
330
if ($ doc ["ok " ]) {
330
331
return $ doc ["values " ];
331
332
}
@@ -335,11 +336,15 @@ public function distinct($fieldName, array $filter = array(), array $options = a
335
336
/**
336
337
* Drop this collection.
337
338
*
338
- * @return Result
339
+ * @see http://docs.mongodb.org/manual/reference/command/drop/
340
+ * @return Cursor
339
341
*/
340
342
public function drop ()
341
343
{
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 );
343
348
}
344
349
345
350
/**
@@ -348,7 +353,7 @@ public function drop()
348
353
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
349
354
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndex/
350
355
* @param string $indexName
351
- * @return Result
356
+ * @return Cursor
352
357
* @throws InvalidArgumentException if "*" is specified
353
358
*/
354
359
public function dropIndex ($ indexName )
@@ -361,7 +366,7 @@ public function dropIndex($indexName)
361
366
*
362
367
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
363
368
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndexes/
364
- * @return Result
369
+ * @return Cursor
365
370
*/
366
371
public function dropIndexes ()
367
372
{
@@ -376,7 +381,7 @@ public function dropIndexes()
376
381
*
377
382
* @param array $filter The find query to execute
378
383
* @param array $options Additional options
379
- * @return Result
384
+ * @return Cursor
380
385
*/
381
386
public function find (array $ filter = array (), array $ options = array ())
382
387
{
@@ -434,7 +439,7 @@ public function findOneAndDelete(array $filter, array $options = array())
434
439
"query " => $ filter ,
435
440
) + $ options ;
436
441
437
- $ doc = $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray ();
442
+ $ doc = current ( $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray () );
438
443
if ($ doc ["ok " ]) {
439
444
return $ doc ["value " ];
440
445
}
@@ -471,7 +476,7 @@ public function findOneAndReplace(array $filter, array $replacement, array $opti
471
476
"query " => $ filter ,
472
477
) + $ options ;
473
478
474
- $ doc = $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray ();
479
+ $ doc = current ( $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray () );
475
480
if ($ doc ["ok " ]) {
476
481
return $ doc ["value " ];
477
482
}
@@ -509,7 +514,7 @@ public function findOneAndUpdate(array $filter, array $update, array $options =
509
514
"query " => $ filter ,
510
515
) + $ options ;
511
516
512
- $ doc = $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray ();
517
+ $ doc = current ( $ this ->_runCommand ($ this ->dbname , $ cmd )->toArray () );
513
518
if ($ doc ["ok " ]) {
514
519
return $ doc ["value " ];
515
520
}
@@ -948,7 +953,7 @@ public function insertOne(array $document)
948
953
*
949
954
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
950
955
* @see http://docs.mongodb.org/manual/reference/method/db.collection.getIndexes/
951
- * @return Result
956
+ * @return Cursor
952
957
*/
953
958
public function listIndexes ()
954
959
{
0 commit comments