BulkWriteResult.php 5.71 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

/**
 * Result class for a bulk write operation.
 */
class BulkWriteResult
{
28
    /** @var WriteResult */
29
    private $writeResult;
30 31

    /** @var mixed[] */
32
    private $insertedIds;
33 34

    /** @var boolean */
35
    private $isAcknowledged;
36 37 38 39 40 41 42 43 44

    /**
     * @param WriteResult $writeResult
     * @param mixed[]     $insertedIds
     */
    public function __construct(WriteResult $writeResult, array $insertedIds)
    {
        $this->writeResult = $writeResult;
        $this->insertedIds = $insertedIds;
45
        $this->isAcknowledged = $writeResult->isAcknowledged();
46 47 48 49 50
    }

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

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
64 65 66 67 68
    }

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

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
82 83 84 85 86
    }

    /**
     * Return a map of the inserted documents' IDs.
     *
87 88 89
     * 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"
90
     * field value. Any driver-generated ID will be a MongoDB\BSON\ObjectId
91
     * instance.
92 93 94 95 96 97 98 99 100 101 102
     *
     * @return mixed[]
     */
    public function getInsertedIds()
    {
        return $this->insertedIds;
    }

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

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
116 117 118 119 120
    }

    /**
     * Return the number of documents that were modified.
     *
121 122 123 124
     * 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.
125 126 127
     *
     * @see BulkWriteResult::isAcknowledged()
     * @return integer|null
128
     * @throws BadMethodCallException is the write result is unacknowledged
129 130 131
     */
    public function getModifiedCount()
    {
132 133 134 135 136
        if ($this->isAcknowledged) {
            return $this->writeResult->getModifiedCount();
        }

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
137 138 139 140 141
    }

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

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
155 156 157 158 159
    }

    /**
     * Return a map of the upserted documents' IDs.
     *
160 161
     * 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
162
     * server did not need to generate an ID), this will contain its "_id". Any
163
     * server-generated ID will be a MongoDB\BSON\ObjectId instance.
164
     *
165 166 167
     * This method should only be called if the write was acknowledged.
     *
     * @see BulkWriteResult::isAcknowledged()
168
     * @return mixed[]
169
     * @throws BadMethodCallException is the write result is unacknowledged
170 171 172
     */
    public function getUpsertedIds()
    {
173 174 175 176 177
        if ($this->isAcknowledged) {
            return $this->writeResult->getUpsertedIds();
        }

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
178 179 180 181 182 183 184 185 186 187 188 189
    }

    /**
     * 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()
    {
190
        return $this->isAcknowledged;
191 192
    }
}