BulkWriteResult.php 5.63 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2015-2017 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
17 18 19 20

namespace MongoDB;

use MongoDB\Driver\WriteResult;
21
use MongoDB\Exception\BadMethodCallException;
22 23 24 25 26 27 28 29

/**
 * Result class for a bulk write operation.
 */
class BulkWriteResult
{
    private $writeResult;
    private $insertedIds;
30
    private $isAcknowledged;
31 32 33 34 35 36 37 38 39

    /**
     * @param WriteResult $writeResult
     * @param mixed[]     $insertedIds
     */
    public function __construct(WriteResult $writeResult, array $insertedIds)
    {
        $this->writeResult = $writeResult;
        $this->insertedIds = $insertedIds;
40
        $this->isAcknowledged = $writeResult->isAcknowledged();
41 42 43 44 45
    }

    /**
     * Return the number of documents that were deleted.
     *
46
     * This method should only be called if the write was acknowledged.
47 48 49
     *
     * @see BulkWriteResult::isAcknowledged()
     * @return integer
50
     * @throws BadMethodCallException is the write result is unacknowledged
51 52 53
     */
    public function getDeletedCount()
    {
54 55 56 57 58
        if ($this->isAcknowledged) {
            return $this->writeResult->getDeletedCount();
        }

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
59 60 61 62 63
    }

    /**
     * Return the number of documents that were inserted.
     *
64
     * This method should only be called if the write was acknowledged.
65 66 67
     *
     * @see BulkWriteResult::isAcknowledged()
     * @return integer
68
     * @throws BadMethodCallException is the write result is unacknowledged
69 70 71
     */
    public function getInsertedCount()
    {
72 73 74 75 76
        if ($this->isAcknowledged) {
            return $this->writeResult->getInsertedCount();
        }

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
77 78 79 80 81
    }

    /**
     * Return a map of the inserted documents' IDs.
     *
82 83 84
     * The index of each ID in the map corresponds to each document's position
     * in the bulk operation. If a document had an ID prior to inserting (i.e.
     * the driver did not generate an ID), the index will contain its "_id"
85
     * field value. Any driver-generated ID will be a MongoDB\BSON\ObjectId
86
     * instance.
87 88 89 90 91 92 93 94 95 96 97
     *
     * @return mixed[]
     */
    public function getInsertedIds()
    {
        return $this->insertedIds;
    }

    /**
     * Return the number of documents that were matched by the filter.
     *
98
     * This method should only be called if the write was acknowledged.
99 100 101
     *
     * @see BulkWriteResult::isAcknowledged()
     * @return integer
102
     * @throws BadMethodCallException is the write result is unacknowledged
103 104 105
     */
    public function getMatchedCount()
    {
106 107 108 109 110
        if ($this->isAcknowledged) {
            return $this->writeResult->getMatchedCount();
        }

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
111 112 113 114 115
    }

    /**
     * Return the number of documents that were modified.
     *
116 117 118 119
     * This value is undefined (i.e. null) if the write executed as a legacy
     * operation instead of command.
     *
     * This method should only be called if the write was acknowledged.
120 121 122
     *
     * @see BulkWriteResult::isAcknowledged()
     * @return integer|null
123
     * @throws BadMethodCallException is the write result is unacknowledged
124 125 126
     */
    public function getModifiedCount()
    {
127 128 129 130 131
        if ($this->isAcknowledged) {
            return $this->writeResult->getModifiedCount();
        }

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
132 133 134 135 136
    }

    /**
     * Return the number of documents that were upserted.
     *
137
     * This method should only be called if the write was acknowledged.
138 139 140
     *
     * @see BulkWriteResult::isAcknowledged()
     * @return integer
141
     * @throws BadMethodCallException is the write result is unacknowledged
142 143 144
     */
    public function getUpsertedCount()
    {
145 146 147 148 149
        if ($this->isAcknowledged) {
            return $this->writeResult->getUpsertedCount();
        }

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
150 151 152 153 154
    }

    /**
     * Return a map of the upserted documents' IDs.
     *
155 156
     * The index of each ID in the map corresponds to each document's position
     * in bulk operation. If a document had an ID prior to upserting (i.e. the
157
     * server did not need to generate an ID), this will contain its "_id". Any
158
     * server-generated ID will be a MongoDB\BSON\ObjectId instance.
159
     *
160 161 162
     * This method should only be called if the write was acknowledged.
     *
     * @see BulkWriteResult::isAcknowledged()
163
     * @return mixed[]
164
     * @throws BadMethodCallException is the write result is unacknowledged
165 166 167
     */
    public function getUpsertedIds()
    {
168 169 170 171 172
        if ($this->isAcknowledged) {
            return $this->writeResult->getUpsertedIds();
        }

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
173 174 175 176 177 178 179 180 181 182 183 184
    }

    /**
     * Return whether this update was acknowledged by the server.
     *
     * If the update was not acknowledged, other fields from the WriteResult
     * (e.g. matchedCount) will be undefined.
     *
     * @return boolean
     */
    public function isAcknowledged()
    {
185
        return $this->isAcknowledged;
186 187
    }
}