BulkWriteResult.php 5.66 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 40 41

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

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

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
61 62 63 64 65
    }

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

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
79 80 81 82 83
    }

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

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

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
113 114 115 116 117
    }

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

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
134 135 136 137 138
    }

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

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
152 153 154 155 156
    }

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

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

    /**
     * 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()
    {
187
        return $this->isAcknowledged;
188 189
    }
}