File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -92,6 +92,32 @@ public function __toString()
92
92
return $ this ->databaseName ;
93
93
}
94
94
95
+ /**
96
+ * Execute a command on this database.
97
+ *
98
+ * @param array|object $command Command document
99
+ * @param ReadPreference|null $readPreference Read preference
100
+ * @return Cursor
101
+ */
102
+ public function command ($ command , ReadPreference $ readPreference = null )
103
+ {
104
+ if ( ! is_array ($ command ) && ! is_object ($ command )) {
105
+ throw new InvalidArgumentTypeException ('$command ' , $ command , 'array or object ' );
106
+ }
107
+
108
+ if ( ! $ command instanceof Command) {
109
+ $ command = new Command ($ command );
110
+ }
111
+
112
+ if ( ! isset ($ readPreference )) {
113
+ $ readPreference = $ this ->readPreference ;
114
+ }
115
+
116
+ $ server = $ this ->manager ->selectServer ($ readPreference );
117
+
118
+ return $ server ->executeCommand ($ this ->databaseName , $ command );
119
+ }
120
+
95
121
/**
96
122
* Create a new collection explicitly.
97
123
*
Original file line number Diff line number Diff line change @@ -64,6 +64,29 @@ public function getGetDatabaseName()
64
64
$ this ->assertEquals ($ this ->getDatabaseName (), $ this ->database ->getDatabaseName ());
65
65
}
66
66
67
+ public function testCommand ()
68
+ {
69
+ $ command = ['isMaster ' => 1 ];
70
+ $ readPreference = new ReadPreference (ReadPreference::RP_PRIMARY );
71
+ $ cursor = $ this ->database ->command ($ command , $ readPreference );
72
+
73
+ $ this ->assertInstanceOf ('MongoDB\Driver\Cursor ' , $ cursor );
74
+ $ commandResult = current ($ cursor ->toArray ());
75
+
76
+ $ this ->assertCommandSucceeded ($ commandResult );
77
+ $ this ->assertTrue (isset ($ commandResult ->ismaster ));
78
+ $ this ->assertTrue ($ commandResult ->ismaster );
79
+ }
80
+
81
+ /**
82
+ * @expectedException MongoDB\Exception\InvalidArgumentTypeException
83
+ * @dataProvider provideInvalidDocumentValues
84
+ */
85
+ public function testCommandCommandArgumentTypeCheck ($ command )
86
+ {
87
+ $ this ->database ->command ($ command );
88
+ }
89
+
67
90
public function testDrop ()
68
91
{
69
92
$ bulkWrite = new BulkWrite ();
You can’t perform that action at this time.
0 commit comments