*/ public $w; /** * @var int
*/
public $wtimeout;
/**
* Creates a new collection
* @link http://www.php.net/manual/en/mongocollection.construct.php
* @param MongoDB $db Parent database.
* @param string $name Name for this collection.
* @throws Exception
* @return MongoCollection
*/
public function __construct(MongoDB $db, $name)
{
$this->db = $db;
$this->name = $name;
$this->collection = $this->db->getDb()->selectCollection($name);
}
/**
* Gets the underlying collection for this object
*
* @internal This part is not of the ext-mongo API and should not be used
* @return \MongoDB\Collection
*/
public function getCollection()
{
return $this->collection;
}
/**
* String representation of this collection
* @link http://www.php.net/manual/en/mongocollection.--tostring.php
* @return string Returns the full name of this collection.
*/
public function __toString()
{
return (string) $this->db . '.' . $this->name;
}
/**
* Gets a collection
* @link http://www.php.net/manual/en/mongocollection.get.php
* @param string $name The next string in the collection name.
* @return MongoCollection
*/
public function __get($name)
{
return $this->db->selectCollection($this->name . '.' . $name);
}
/**
* @link http://www.php.net/manual/en/mongocollection.aggregate.php
* @param array $pipeline
* @param array $op
* @param array $pipelineOperators
* @return array
*/
public function aggregate(array $pipeline, array $op, array $pipelineOperators)
{
$this->notImplemented();
}
/**
* @link http://php.net/manual/en/mongocollection.aggregatecursor.php
* @param array $pipeline
* @param array $options
* @return MongoCommandCursor
*/
public function aggregateCursor(array $pipeline, array $options)
{
return $this->collection->aggregate($pipeline, $options);
}
/**
* Returns this collection's name
* @link http://www.php.net/manual/en/mongocollection.getname.php
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @link http://www.php.net/manual/en/mongocollection.getslaveokay.php
* @return bool
*/
public function getSlaveOkay()
{
$this->notImplemented();
}
/**
* @link http://www.php.net/manual/en/mongocollection.setslaveokay.php
* @param bool $ok
* @return bool
*/
public function setSlaveOkay($ok = true)
{
$this->notImplemented();
}
/**
* @link http://www.php.net/manual/en/mongocollection.getreadpreference.php
* @return array
*/
public function getReadPreference()
{
$this->notImplemented();
}
/**
* @param string $read_preference
* @param array $tags
* @return bool
*/
public function setReadPreference($read_preference, array $tags)
{
$this->notImplemented();
}
/**
* Drops this collection
* @link http://www.php.net/manual/en/mongocollection.drop.php
* @return array Returns the database response.
*/
public function drop()
{
return $this->collection->drop();
}
/**
* Validates this collection
* @link http://www.php.net/manual/en/mongocollection.validate.php
* @param bool $scan_data Only validate indices, not the base collection.
* @return array Returns the database's evaluation of this object.
*/
public function validate($scan_data = FALSE)
{
$this->notImplemented();
}
/**
* Inserts an array into the collection
* @link http://www.php.net/manual/en/mongocollection.insert.php
* @param array|object $a
* @param array $options
* @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.
* @throws MongoCursorException if the "w" option is set and the write fails.
* @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.
* @return bool|array Returns an array containing the status of the insertion if the "w" option is set.
*/
public function insert($a, array $options = array())
{
return $this->collection->insertOne(TypeConverter::convertLegacyArrayToObject($a), $options);
}
/**
* Inserts multiple documents into this collection
* @link http://www.php.net/manual/en/mongocollection.batchinsert.php
* @param array $a An array of arrays.
* @param array $options Options for the inserts.
* @throws MongoCursorException
* @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.
*/
public function batchInsert(array $a, array $options = array())
{
return $this->collection->insertMany($a, $options);
}
/**
* Update records based on a given criteria
* @link http://www.php.net/manual/en/mongocollection.update.php
* @param array $criteria Description of the objects to update.
* @param array $newobj The object with which to update the matching records.
* @param array $options This parameter is an associative array of the form
* array("optionname" => boolean, ...).
*
* Currently supported options are:
* "upsert": If no document matches $$criteria, a new document will be created from $$criteria and $$new_object (see upsert example).
*
* "multiple": All documents matching $criteria will be updated. MongoCollection::update has exactly the opposite behavior of MongoCollection::remove- it updates one document by
* 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
* database may change its default behavior at some point in the future.
*
* "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
* 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
* 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).
* 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.
* 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).
* This overrides the w variable set on the collection.
*
* "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.
*
* "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
* not respond within the timeout period, a MongoCursorTimeoutException will be thrown
* @throws MongoCursorException
* @return boolean
*/
public function update(array $criteria , array $newobj, array $options = array())
{
$multiple = ($options['multiple']) ? $options['multiple'] : false;
// $multiple = $options['multiple'] ?? false;
$method = $multiple ? 'updateMany' : 'updateOne';
return $this->collection->$method($criteria, $newobj, $options);
}
/**
* (PECL mongo >= 0.9.0)
* Remove records from this collection
* @link http://www.php.net/manual/en/mongocollection.remove.php
* @param array $criteria [optional]
Query criteria for the documents to delete.
* @param array $options [optional]An array of options for the remove operation. Currently available options * include: *
"w"
See {@link http://www.php.net/manual/en/mongo.writeconcerns.php Write Concerns}. The default value for MongoClient is 1.
* "justOne" *
*
* Specify TRUE to limit deletion to just one document. If FALSE or
* omitted, all documents matching the criteria will be deleted.
*
"fsync"
Boolean, defaults to FALSE. If journaling is enabled, it works exactly like "j". If journaling is not enabled, the write operation blocks until it is synced to database files on disk. If TRUE, an acknowledged insert is implied and this option will override setting "w" to 0.
Note: If journaling is enabled, users are strongly encouraged to use the "j" option instead of "fsync". Do not use "fsync" and "j" simultaneously, as that will result in an error.
"j"
Boolean, defaults to FALSE. Forces the write operation to block until it is synced to the journal on disk. If TRUE, an acknowledged write is implied and this option will override setting "w" to 0.
Note: 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.
"socketTimeoutMS"
This option specifies the time limit, in milliseconds, for socket communication. If the server does not respond within the timeout period, a MongoCursorTimeoutException will be thrown and there will be no way to determine if the server actually handled the write or not. A value of -1 may be specified to block indefinitely. The default value for MongoClient is 30000 (30 seconds).
"w"
See {@link http://www.php.net/manual/en/mongo.writeconcerns.php Write Concerns }. The default value for MongoClient is 1.
"wTimeoutMS"
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 "w" is greater than 1, as the timeout pertains to replication. If the write concern is not satisfied within the time limit, a MongoCursorException will be thrown. A value of 0 may be specified to block indefinitely. The default value for {@link http://www.php.net/manual/en/class.mongoclient.php MongoClient} is 10000 (ten seconds).
* The following options are deprecated and should no longer be used: *
"safe"
Deprecated. Please use the {@link http://www.php.net/manual/en/mongo.writeconcerns.php write concern} "w" option.
"timeout"
Deprecated alias for "socketTimeoutMS".
"wtimeout"
Deprecated alias for "wTimeoutMS".
* @throws MongoCursorException * @throws MongoCursorTimeoutException * @return bool|arrayReturns an array containing the status of the removal if the * "w" option is set. Otherwise, returns TRUE. *
** Fields in the status array are described in the documentation for * MongoCollection::insert(). *
*/ public function remove(array $criteria = array(), array $options = array()) { $multiple = isset($options['justOne']) ? !$options['justOne'] : false; // $multiple = !$options['justOne'] ?? false; $method = $multiple ? 'deleteMany' : 'deleteOne'; return $this->collection->$method($criteria, $options); } /** * Querys this collection * @link http://www.php.net/manual/en/mongocollection.find.php * @param array $query The fields for which to search. * @param array $fields Fields of the results to return. * @return MongoCursor */ public function find(array $query = array(), array $fields = array()) { $cursor = $this->collection->find($query); return new MongoCursor($this->db->getConnection(), (string) $this, $query, $fields, $cursor); } /** * Retrieve a list of distinct values for the given key across a collection * @link http://www.php.net/manual/ru/mongocollection.distinct.php * @param string $key The key to use. * @param array $query An optional query parameters * @return array|bool Returns an array of distinct values, or FALSE on failure */ public function distinct($key, array $query = NULL) { return array_map([TypeConverter::class, 'convertToLegacyType'], $this->collection->distinct($key, $query)); } /** * Update a document and return it * @link http://www.php.net/manual/ru/mongocollection.findandmodify.php * @param array $query The query criteria to search for. * @param array $update The update criteria. * @param array $fields Optionally only return these fields. * @param array $options An array of options to apply, such as remove the match document from the DB and return it. * @return array Returns the original document, or the modified document when new is set. */ public function findAndModify(array $query, array $update = NULL, array $fields = NULL, array $options = NULL) { } /** * Querys this collection, returning a single element * @link http://www.php.net/manual/en/mongocollection.findone.php * @param array $query The fields for which to search. * @param array $fields Fields of the results to return. * @return array|null */ public function findOne(array $query = array(), array $fields = array()) { $document = $this->collection->findOne(TypeConverter::convertLegacyArrayToObject($query)); if ($document !== null) { $document = TypeConverter::convertObjectToLegacyArray($document); } return $document; } /** * Creates an index on the given field(s), or does nothing if the index already exists * @link http://www.php.net/manual/en/mongocollection.createindex.php * @param array $keys Field or fields to use as index. * @param array $options [optional] This parameter is an associative array of the form array("optionname" =>