/** * @param $document * @return array * @throws MongoDBException */ public function getShardKeyQuery($document) { $shardKeysQueryPart = array(); $dcs = $this->uow->getDocumentChangeSet($document); $data = $this->uow->getDocumentActualData($document); $md = $this->dm->getMetadataFactory()->getMetadataFor(get_class($document)); $keys = $md->shardKeys; $fieldMappings = $this->dm->getClassMetadata(get_class($document))->fieldMappings; foreach ($keys as $key) { if ($key !== 'id') { $queryKey = $fieldMappings[$key]['name']; } else { $queryKey = '_id'; } //If the document is new, we can ignore shard key value, otherwise throw exception $isUpdate = $this->uow->isScheduledForUpdate($document); if ($isUpdate && isset($dcs[$key]) && $dcs[$key][0] !== null && $dcs[$key][0] != $dcs[$key][1] && (!isset($options['upsert']) || isset($options['upsert']) && $options['upsert'] === true)) { throw MongoDBException::shardKeyChange($key); } if (!isset($data[$key])) { throw MongoDBException::shardKeyMissing($key); } $new = $data[$key]; $mapping = $fieldMappings[$key]; // @Field, @String, @Date, etc. if (!isset($mapping['association'])) { if (isset($new) || $mapping['nullable'] === true) { $shardKeysQueryPart[$queryKey] = is_null($new) ? null : Type::getType($mapping['type'])->convertToDatabaseValue($new); } // @ReferenceOne } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE && $mapping['isOwningSide']) { if (isset($new) || $mapping['nullable'] === true) { $shardKeysQueryPart[$queryKey] = is_null($new) ? null : $this->pb->prepareReferencedDocumentValue($mapping, $new); } // @ReferenceMany } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_MANY) { // Do nothing right now } } return $shardKeysQueryPart; }