MongoCollection.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. */
  15. use Alcaeus\MongoDbAdapter\Helper;
  16. use Alcaeus\MongoDbAdapter\TypeConverter;
  17. /**
  18. * Represents a database collection.
  19. * @link http://www.php.net/manual/en/class.mongocollection.php
  20. */
  21. class MongoCollection
  22. {
  23. use Helper\ReadPreference;
  24. use Helper\WriteConcern;
  25. const ASCENDING = 1;
  26. const DESCENDING = -1;
  27. /**
  28. * @var MongoDB
  29. */
  30. public $db = NULL;
  31. /**
  32. * @var string
  33. */
  34. protected $name;
  35. /**
  36. * @var \MongoDB\Collection
  37. */
  38. protected $collection;
  39. /**
  40. * Creates a new collection
  41. * @link http://www.php.net/manual/en/mongocollection.construct.php
  42. * @param MongoDB $db Parent database.
  43. * @param string $name Name for this collection.
  44. * @throws Exception
  45. * @return MongoCollection
  46. */
  47. public function __construct(MongoDB $db, $name)
  48. {
  49. $this->db = $db;
  50. $this->name = $name;
  51. $this->setReadPreferenceFromArray($db->getReadPreference());
  52. $this->setWriteConcernFromArray($db->getWriteConcern());
  53. $this->createCollectionObject();
  54. }
  55. /**
  56. * Gets the underlying collection for this object
  57. *
  58. * @internal This part is not of the ext-mongo API and should not be used
  59. * @return \MongoDB\Collection
  60. */
  61. public function getCollection()
  62. {
  63. return $this->collection;
  64. }
  65. /**
  66. * String representation of this collection
  67. * @link http://www.php.net/manual/en/mongocollection.--tostring.php
  68. * @return string Returns the full name of this collection.
  69. */
  70. public function __toString()
  71. {
  72. return (string) $this->db . '.' . $this->name;
  73. }
  74. /**
  75. * Gets a collection
  76. * @link http://www.php.net/manual/en/mongocollection.get.php
  77. * @param string $name The next string in the collection name.
  78. * @return MongoCollection
  79. */
  80. public function __get($name)
  81. {
  82. // Handle w and wtimeout properties that replicate data stored in $readPreference
  83. if ($name === 'w' || $name === 'wtimeout') {
  84. return $this->getWriteConcern()[$name];
  85. }
  86. return $this->db->selectCollection($this->name . '.' . $name);
  87. }
  88. /**
  89. * @param string $name
  90. * @param mixed $value
  91. */
  92. public function __set($name, $value)
  93. {
  94. if ($name === 'w' || $name === 'wtimeout') {
  95. $this->setWriteConcernFromArray([$name => $value] + $this->getWriteConcern());
  96. $this->createCollectionObject();
  97. }
  98. }
  99. /**
  100. * @link http://www.php.net/manual/en/mongocollection.aggregate.php
  101. * @param array $pipeline
  102. * @param array $op
  103. * @return array
  104. */
  105. public function aggregate(array $pipeline, array $op = [])
  106. {
  107. if (! TypeConverter::isNumericArray($pipeline)) {
  108. $pipeline = [];
  109. $options = [];
  110. $i = 0;
  111. foreach (func_get_args() as $operator) {
  112. $i++;
  113. if (! is_array($operator)) {
  114. trigger_error("Argument $i is not an array", E_WARNING);
  115. return;
  116. }
  117. $pipeline[] = $operator;
  118. }
  119. } else {
  120. $options = $op;
  121. }
  122. $command = [
  123. 'aggregate' => $this->name,
  124. 'pipeline' => $pipeline
  125. ];
  126. $command += $options;
  127. return $this->db->command($command, [], $hash);
  128. }
  129. /**
  130. * @link http://php.net/manual/en/mongocollection.aggregatecursor.php
  131. * @param array $pipeline
  132. * @param array $options
  133. * @return MongoCommandCursor
  134. */
  135. public function aggregateCursor(array $pipeline, array $options = [])
  136. {
  137. // Build command manually, can't use mongo-php-library here
  138. $command = [
  139. 'aggregate' => $this->name,
  140. 'pipeline' => $pipeline
  141. ];
  142. // Convert cursor option
  143. if (! isset($options['cursor']) || $options['cursor'] === true || $options['cursor'] === []) {
  144. // Cursor option needs to be an object convert bools and empty arrays since those won't be handled by TypeConverter
  145. $options['cursor'] = new \stdClass;
  146. }
  147. $command += $options;
  148. $cursor = new MongoCommandCursor($this->db->getConnection(), (string)$this, $command);
  149. $cursor->setReadPreference($this->getReadPreference());
  150. return $cursor;
  151. }
  152. /**
  153. * Returns this collection's name
  154. * @link http://www.php.net/manual/en/mongocollection.getname.php
  155. * @return string
  156. */
  157. public function getName()
  158. {
  159. return $this->name;
  160. }
  161. /**
  162. * @link http://www.php.net/manual/en/mongocollection.getslaveokay.php
  163. * @return bool
  164. */
  165. public function getSlaveOkay()
  166. {
  167. $this->notImplemented();
  168. }
  169. /**
  170. * @link http://www.php.net/manual/en/mongocollection.setslaveokay.php
  171. * @param bool $ok
  172. * @return bool
  173. */
  174. public function setSlaveOkay($ok = true)
  175. {
  176. $this->notImplemented();
  177. }
  178. /**
  179. * {@inheritdoc}
  180. */
  181. public function setReadPreference($readPreference, $tags = null)
  182. {
  183. $result = $this->setReadPreferenceFromParameters($readPreference, $tags);
  184. $this->createCollectionObject();
  185. return $result;
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function setWriteConcern($wstring, $wtimeout = 0)
  191. {
  192. $result = $this->setWriteConcernFromParameters($wstring, $wtimeout);
  193. $this->createCollectionObject();
  194. return $result;
  195. }
  196. /**
  197. * Drops this collection
  198. * @link http://www.php.net/manual/en/mongocollection.drop.php
  199. * @return array Returns the database response.
  200. */
  201. public function drop()
  202. {
  203. return $this->collection->drop();
  204. }
  205. /**
  206. * Validates this collection
  207. * @link http://www.php.net/manual/en/mongocollection.validate.php
  208. * @param bool $scan_data Only validate indices, not the base collection.
  209. * @return array Returns the database's evaluation of this object.
  210. */
  211. public function validate($scan_data = FALSE)
  212. {
  213. $this->notImplemented();
  214. }
  215. /**
  216. * Inserts an array into the collection
  217. * @link http://www.php.net/manual/en/mongocollection.insert.php
  218. * @param array|object $a
  219. * @param array $options
  220. * @throws MongoException if the inserted document is empty or if it contains zero-length keys. Attempting to insert an object with protected and private properties will cause a zero-length key error.
  221. * @throws MongoCursorException if the "w" option is set and the write fails.
  222. * @throws MongoCursorTimeoutException if the "w" option is set to a value greater than one and the operation takes longer than MongoCursor::$timeout milliseconds to complete. This does not kill the operation on the server, it is a client-side timeout. The operation in MongoCollection::$wtimeout is milliseconds.
  223. * @return bool|array Returns an array containing the status of the insertion if the "w" option is set.
  224. */
  225. public function insert($a, array $options = array())
  226. {
  227. return $this->collection->insertOne(TypeConverter::convertLegacyArrayToObject($a), $options);
  228. }
  229. /**
  230. * Inserts multiple documents into this collection
  231. * @link http://www.php.net/manual/en/mongocollection.batchinsert.php
  232. * @param array $a An array of arrays.
  233. * @param array $options Options for the inserts.
  234. * @throws MongoCursorException
  235. * @return mixed f "safe" is set, returns an associative array with the status of the inserts ("ok") and any error that may have occured ("err"). Otherwise, returns TRUE if the batch insert was successfully sent, FALSE otherwise.
  236. */
  237. public function batchInsert(array $a, array $options = array())
  238. {
  239. return $this->collection->insertMany($a, $options);
  240. }
  241. /**
  242. * Update records based on a given criteria
  243. * @link http://www.php.net/manual/en/mongocollection.update.php
  244. * @param array $criteria Description of the objects to update.
  245. * @param array $newobj The object with which to update the matching records.
  246. * @param array $options This parameter is an associative array of the form
  247. * array("optionname" => boolean, ...).
  248. *
  249. * Currently supported options are:
  250. * "upsert": If no document matches $$criteria, a new document will be created from $$criteria and $$new_object (see upsert example).
  251. *
  252. * "multiple": All documents matching $criteria will be updated. MongoCollection::update has exactly the opposite behavior of MongoCollection::remove- it updates one document by
  253. * default, not all matching documents. It is recommended that you always specify whether you want to update multiple documents or a single document, as the
  254. * database may change its default behavior at some point in the future.
  255. *
  256. * "safe" Can be a boolean or integer, defaults to false. If false, the program continues executing without waiting for a database response. If true, the program will wait for
  257. * the database response and throw a MongoCursorException if the update did not succeed. If you are using replication and the master has changed, using "safe" will make the driver
  258. * disconnect from the master, throw and exception, and attempt to find a new master on the next operation (your application must decide whether or not to retry the operation on the new master).
  259. * If you do not use "safe" with a replica set and the master changes, there will be no way for the driver to know about the change so it will continuously and silently fail to write.
  260. * If safe is an integer, will replicate the update to that many machines before returning success (or throw an exception if the replication times out, see wtimeout).
  261. * This overrides the w variable set on the collection.
  262. *
  263. * "fsync": Boolean, defaults to false. Forces the update to be synced to disk before returning success. If true, a safe update is implied and will override setting safe to false.
  264. *
  265. * "timeout" Integer, defaults to MongoCursor::$timeout. If "safe" is set, this sets how long (in milliseconds) for the client to wait for a database response. If the database does
  266. * not respond within the timeout period, a MongoCursorTimeoutException will be thrown
  267. * @throws MongoCursorException
  268. * @return boolean
  269. */
  270. public function update(array $criteria , array $newobj, array $options = array())
  271. {
  272. $multiple = ($options['multiple']) ? $options['multiple'] : false;
  273. // $multiple = $options['multiple'] ?? false;
  274. $method = $multiple ? 'updateMany' : 'updateOne';
  275. return $this->collection->$method($criteria, $newobj, $options);
  276. }
  277. /**
  278. * (PECL mongo &gt;= 0.9.0)<br/>
  279. * Remove records from this collection
  280. * @link http://www.php.net/manual/en/mongocollection.remove.php
  281. * @param array $criteria [optional] <p>Query criteria for the documents to delete.</p>
  282. * @param array $options [optional] <p>An array of options for the remove operation. Currently available options
  283. * include:
  284. * </p><ul>
  285. * <li><p><em>"w"</em></p><p>See {@link http://www.php.net/manual/en/mongo.writeconcerns.php Write Concerns}. The default value for <b>MongoClient</b> is <em>1</em>.</p></li>
  286. * <li>
  287. * <p>
  288. * <em>"justOne"</em>
  289. * </p>
  290. * <p>
  291. * Specify <strong><code>TRUE</code></strong> to limit deletion to just one document. If <strong><code>FALSE</code></strong> or
  292. * omitted, all documents matching the criteria will be deleted.
  293. * </p>
  294. * </li>
  295. * <li><p><em>"fsync"</em></p><p>Boolean, defaults to <b>FALSE</b>. If journaling is enabled, it works exactly like <em>"j"</em>. If journaling is not enabled, the write operation blocks until it is synced to database files on disk. If <strong><code>TRUE</code></strong>, an acknowledged insert is implied and this option will override setting <em>"w"</em> to <em>0</em>.</p><blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">If journaling is enabled, users are strongly encouraged to use the <em>"j"</em> option instead of <em>"fsync"</em>. Do not use <em>"fsync"</em> and <em>"j"</em> simultaneously, as that will result in an error.</p></blockquote></li>
  296. * <li><p><em>"j"</em></p><p>Boolean, defaults to <b>FALSE</b>. Forces the write operation to block until it is synced to the journal on disk. If <strong><code>TRUE</code></strong>, an acknowledged write is implied and this option will override setting <em>"w"</em> to <em>0</em>.</p><blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">If this option is used and journaling is disabled, MongoDB 2.6+ will raise an error and the write will fail; older server versions will simply ignore the option.</p></blockquote></li>
  297. * <li><p><em>"socketTimeoutMS"</em></p><p>This option specifies the time limit, in milliseconds, for socket communication. If the server does not respond within the timeout period, a <b>MongoCursorTimeoutException</b> will be thrown and there will be no way to determine if the server actually handled the write or not. A value of <em>-1</em> may be specified to block indefinitely. The default value for <b>MongoClient</b> is <em>30000</em> (30 seconds).</p></li>
  298. * <li><p><em>"w"</em></p><p>See {@link http://www.php.net/manual/en/mongo.writeconcerns.php Write Concerns }. The default value for <b>MongoClient</b> is <em>1</em>.</p></li>
  299. * <li><p><em>"wTimeoutMS"</em></p><p>This option specifies the time limit, in milliseconds, for {@link http://www.php.net/manual/en/mongo.writeconcerns.php write concern} acknowledgement. It is only applicable when <em>"w"</em> is greater than <em>1</em>, as the timeout pertains to replication. If the write concern is not satisfied within the time limit, a <a href="class.mongocursorexception.php" class="classname">MongoCursorException</a> will be thrown. A value of <em>0</em> may be specified to block indefinitely. The default value for {@link http://www.php.net/manual/en/class.mongoclient.php MongoClient} is <em>10000</em> (ten seconds).</p></li>
  300. * </ul>
  301. *
  302. * <p>
  303. * The following options are deprecated and should no longer be used:
  304. * </p><ul>
  305. * <li><p><em>"safe"</em></p><p>Deprecated. Please use the {@link http://www.php.net/manual/en/mongo.writeconcerns.php write concern} <em>"w"</em> option.</p></li>
  306. * <li><p><em>"timeout"</em></p><p>Deprecated alias for <em>"socketTimeoutMS"</em>.</p></li>
  307. * <li><p><b>"wtimeout"</b></p><p>Deprecated alias for <em>"wTimeoutMS"</em>.</p></p>
  308. * @throws MongoCursorException
  309. * @throws MongoCursorTimeoutException
  310. * @return bool|array <p>Returns an array containing the status of the removal if the
  311. * <em>"w"</em> option is set. Otherwise, returns <b>TRUE</b>.
  312. * </p>
  313. * <p>
  314. * Fields in the status array are described in the documentation for
  315. * <b>MongoCollection::insert()</b>.
  316. * </p>
  317. */
  318. public function remove(array $criteria = array(), array $options = array())
  319. {
  320. $multiple = isset($options['justOne']) ? !$options['justOne'] : false;
  321. // $multiple = !$options['justOne'] ?? false;
  322. $method = $multiple ? 'deleteMany' : 'deleteOne';
  323. return $this->collection->$method($criteria, $options);
  324. }
  325. /**
  326. * Querys this collection
  327. * @link http://www.php.net/manual/en/mongocollection.find.php
  328. * @param array $query The fields for which to search.
  329. * @param array $fields Fields of the results to return.
  330. * @return MongoCursor
  331. */
  332. public function find(array $query = array(), array $fields = array())
  333. {
  334. $cursor = new MongoCursor($this->db->getConnection(), (string)$this, $query, $fields);
  335. $cursor->setReadPreference($this->getReadPreference());
  336. return $cursor;
  337. }
  338. /**
  339. * Retrieve a list of distinct values for the given key across a collection
  340. * @link http://www.php.net/manual/ru/mongocollection.distinct.php
  341. * @param string $key The key to use.
  342. * @param array $query An optional query parameters
  343. * @return array|bool Returns an array of distinct values, or <b>FALSE</b> on failure
  344. */
  345. public function distinct($key, array $query = [])
  346. {
  347. return array_map([TypeConverter::class, 'convertToLegacyType'], $this->collection->distinct($key, $query));
  348. }
  349. /**
  350. * Update a document and return it
  351. * @link http://www.php.net/manual/ru/mongocollection.findandmodify.php
  352. * @param array $query The query criteria to search for.
  353. * @param array $update The update criteria.
  354. * @param array $fields Optionally only return these fields.
  355. * @param array $options An array of options to apply, such as remove the match document from the DB and return it.
  356. * @return array Returns the original document, or the modified document when new is set.
  357. */
  358. public function findAndModify(array $query, array $update = NULL, array $fields = NULL, array $options = NULL)
  359. {
  360. }
  361. /**
  362. * Querys this collection, returning a single element
  363. * @link http://www.php.net/manual/en/mongocollection.findone.php
  364. * @param array $query The fields for which to search.
  365. * @param array $fields Fields of the results to return.
  366. * @return array|null
  367. */
  368. public function findOne(array $query = array(), array $fields = array())
  369. {
  370. $document = $this->collection->findOne(TypeConverter::convertLegacyArrayToObject($query), ['projection' => $fields]);
  371. if ($document !== null) {
  372. $document = TypeConverter::convertObjectToLegacyArray($document);
  373. }
  374. return $document;
  375. }
  376. /**
  377. * Creates an index on the given field(s), or does nothing if the index already exists
  378. * @link http://www.php.net/manual/en/mongocollection.createindex.php
  379. * @param array $keys Field or fields to use as index.
  380. * @param array $options [optional] This parameter is an associative array of the form array("optionname" => <boolean>, ...).
  381. * @return array Returns the database response.
  382. */
  383. public function createIndex(array $keys, array $options = array()) {}
  384. /**
  385. * @deprecated Use MongoCollection::createIndex() instead.
  386. * Creates an index on the given field(s), or does nothing if the index already exists
  387. * @link http://www.php.net/manual/en/mongocollection.ensureindex.php
  388. * @param array $keys Field or fields to use as index.
  389. * @param array $options [optional] This parameter is an associative array of the form array("optionname" => <boolean>, ...).
  390. * @return boolean always true
  391. */
  392. public function ensureIndex(array $keys, array $options = array()) {}
  393. /**
  394. * Deletes an index from this collection
  395. * @link http://www.php.net/manual/en/mongocollection.deleteindex.php
  396. * @param string|array $keys Field or fields from which to delete the index.
  397. * @return array Returns the database response.
  398. */
  399. public function deleteIndex($keys) {}
  400. /**
  401. * Delete all indexes for this collection
  402. * @link http://www.php.net/manual/en/mongocollection.deleteindexes.php
  403. * @return array Returns the database response.
  404. */
  405. public function deleteIndexes() {}
  406. /**
  407. * Returns an array of index names for this collection
  408. * @link http://www.php.net/manual/en/mongocollection.getindexinfo.php
  409. * @return array Returns a list of index names.
  410. */
  411. public function getIndexInfo() {}
  412. /**
  413. * Counts the number of documents in this collection
  414. * @link http://www.php.net/manual/en/mongocollection.count.php
  415. * @param array|stdClass $query
  416. * @return int Returns the number of documents matching the query.
  417. */
  418. public function count($query = array())
  419. {
  420. return $this->collection->count($query);
  421. }
  422. /**
  423. * Saves an object to this collection
  424. * @link http://www.php.net/manual/en/mongocollection.save.php
  425. * @param array|object $a Array to save. If an object is used, it may not have protected or private properties.
  426. * Note: If the parameter does not have an _id key or property, a new MongoId instance will be created and assigned to it.
  427. * See MongoCollection::insert() for additional information on this behavior.
  428. * @param array $options Options for the save.
  429. * <dl>
  430. * <dt>"w"
  431. * <dd>See WriteConcerns. The default value for MongoClient is 1.
  432. * <dt>"fsync"
  433. * <dd>Boolean, defaults to FALSE. Forces the insert to be synced to disk before returning success. If TRUE, an acknowledged insert is implied and will override setting w to 0.
  434. * <dt>"timeout"
  435. * <dd>Integer, defaults to MongoCursor::$timeout. If "safe" is set, this sets how long (in milliseconds) for the client to wait for a database response. If the database does not respond within the timeout period, a MongoCursorTimeoutException will be thrown.
  436. * <dt>"safe"
  437. * <dd>Deprecated. Please use the WriteConcern w option.
  438. * </dl>
  439. * @throws MongoException if the inserted document is empty or if it contains zero-length keys. Attempting to insert an object with protected and private properties will cause a zero-length key error.
  440. * @throws MongoCursorException if the "w" option is set and the write fails.
  441. * @throws MongoCursorTimeoutException if the "w" option is set to a value greater than one and the operation takes longer than MongoCursor::$timeout milliseconds to complete. This does not kill the operation on the server, it is a client-side timeout. The operation in MongoCollection::$wtimeout is milliseconds.
  442. * @return array|boolean If w was set, returns an array containing the status of the save.
  443. * Otherwise, returns a boolean representing if the array was not empty (an empty array will not be inserted).
  444. */
  445. public function save($a, array $options = array()) {}
  446. /**
  447. * Creates a database reference
  448. * @link http://www.php.net/manual/en/mongocollection.createdbref.php
  449. * @param array $a Object to which to create a reference.
  450. * @return array Returns a database reference array.
  451. */
  452. public function createDBRef(array $a) {}
  453. /**
  454. * Fetches the document pointed to by a database reference
  455. * @link http://www.php.net/manual/en/mongocollection.getdbref.php
  456. * @param array $ref A database reference.
  457. * @return array Returns the database document pointed to by the reference.
  458. */
  459. public function getDBRef(array $ref) {}
  460. /**
  461. * @param mixed $keys
  462. * @static
  463. * @return string
  464. */
  465. protected static function toIndexString($keys) {}
  466. /**
  467. * Performs an operation similar to SQL's GROUP BY command
  468. * @link http://www.php.net/manual/en/mongocollection.group.php
  469. * @param mixed $keys Fields to group by. If an array or non-code object is passed, it will be the key used to group results.
  470. * @param array $initial Initial value of the aggregation counter object.
  471. * @param MongoCode $reduce A function that aggregates (reduces) the objects iterated.
  472. * @param array $condition An condition that must be true for a row to be considered.
  473. * @return array
  474. */
  475. public function group($keys, array $initial, MongoCode $reduce, array $condition = array()) {}
  476. protected function notImplemented()
  477. {
  478. throw new \Exception('Not implemented');
  479. }
  480. /**
  481. * @return \MongoDB\Collection
  482. */
  483. private function createCollectionObject()
  484. {
  485. $options = [
  486. 'readPreference' => $this->readPreference,
  487. 'writeConcern' => $this->writeConcern,
  488. ];
  489. if ($this->collection === null) {
  490. $this->collection = $this->db->getDb()->selectCollection($this->name, $options);
  491. } else {
  492. $this->collection = $this->collection->withOptions($options);
  493. }
  494. }
  495. }