/**
  * Loads an document by a list of field criteria.
  *
  * @param array $criteria The criteria by which to load the document.
  * @param object $document The document to load the data into. If not specified,
  *        a new document is created.
  * @param array $hints Hints for document creation.
  * @param int $lockMode
  * @param array $sort
  * @return object The loaded and managed document instance or NULL if the document can not be found.
  * @todo Check identity map? loadById method? Try to guess whether $criteria is the id?
  */
 public function load($criteria, $document = null, array $hints = array(), $lockMode = 0, array $sort = array())
 {
     $criteria = $this->prepareQuery($criteria);
     $cursor = $this->collection->find($criteria)->limit(1);
     if ($sort) {
         $cursor->sort($sort);
     }
     $result = $cursor->getSingleResult();
     if ($this->class->isLockable) {
         $lockMapping = $this->class->fieldMappings[$this->class->lockField];
         if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) {
             throw LockException::lockFailed($result);
         }
     }
     return $this->createDocument($result, $document, $hints);
 }
 /**
  * Finds a document by a set of criteria.
  *
  * If a scalar or MongoId is provided for $criteria, it will be used to
  * match an _id value.
  *
  * @param mixed   $criteria Query criteria
  * @param object  $document Document to load the data into. If not specified, a new document is created.
  * @param array   $hints    Hints for document creation
  * @param integer $lockMode
  * @param array   $sort     Sort array for Cursor::sort()
  * @throws \Doctrine\ODM\MongoDB\LockException
  * @return object|null The loaded and managed document instance or null if no document was found
  * @todo Check identity map? loadById method? Try to guess whether $criteria is the id?
  */
 public function load($criteria, $document = null, array $hints = array(), $lockMode = 0, array $sort = null)
 {
     // TODO: remove this
     if ($criteria === null || is_scalar($criteria) || $criteria instanceof \MongoId) {
         $criteria = array('_id' => $criteria);
     }
     $criteria = $this->prepareQueryOrNewObj($criteria);
     $criteria = $this->addDiscriminatorToPreparedQuery($criteria);
     $criteria = $this->addFilterToPreparedQuery($criteria);
     $cursor = $this->collection->find($criteria);
     if (null !== $sort) {
         $cursor->sort($this->prepareSortOrProjection($sort));
     }
     $result = $cursor->getSingleResult();
     if ($this->class->isLockable) {
         $lockMapping = $this->class->fieldMappings[$this->class->lockField];
         if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) {
             throw LockException::lockFailed($result);
         }
     }
     return $this->createDocument($result, $document, $hints);
 }
 /**
  * Executes a query updating the given document.
  *
  * @param object $document
  * @param array $newObj
  * @param array $options
  */
 private function executeQuery($document, array $newObj, array $options)
 {
     $className = get_class($document);
     $class = $this->dm->getClassMetadata($className);
     $id = $class->getDatabaseIdentifierValue($this->uow->getDocumentIdentifier($document));
     $query = array('_id' => $id);
     if ($class->isVersioned) {
         $query[$class->versionField] = $class->reflFields[$class->versionField]->getValue($document);
     }
     $collection = $this->dm->getDocumentCollection($className);
     $result = $collection->update($query, $newObj, $options);
     if ($class->isVersioned && !$result['n']) {
         throw LockException::lockFailed($document);
     }
 }