insert_many.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2014 MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <cstdint>
  16. #include <map>
  17. #include <bsoncxx/array/value.hpp>
  18. #include <bsoncxx/types.hpp>
  19. #include <mongocxx/result/bulk_write.hpp>
  20. #include <mongocxx/config/prelude.hpp>
  21. namespace mongocxx {
  22. MONGOCXX_INLINE_NAMESPACE_BEGIN
  23. class collection;
  24. namespace result {
  25. ///
  26. /// Class representing the result of a MongoDB insert many operation
  27. /// (executed as a bulk write).
  28. ///
  29. class MONGOCXX_API insert_many {
  30. public:
  31. using id_map = std::map<std::size_t, bsoncxx::document::element>;
  32. insert_many(result::bulk_write result, bsoncxx::array::value inserted_ids);
  33. insert_many(const insert_many&);
  34. insert_many(insert_many&&) = default;
  35. insert_many& operator=(const insert_many&);
  36. insert_many& operator=(insert_many&&) = default;
  37. ///
  38. /// Returns the bulk write result for this insert many operation.
  39. ///
  40. /// @return The raw bulk write result.
  41. ///
  42. const result::bulk_write& result() const;
  43. ///
  44. /// Gets the number of documents that were inserted during this operation.
  45. ///
  46. /// @return The number of documents that were inserted.
  47. ///
  48. std::int32_t inserted_count() const;
  49. ///
  50. /// Gets the _ids of the inserted documents.
  51. ///
  52. /// @note The returned id_map must not be accessed after the result::insert_many object is
  53. /// destroyed.
  54. /// @return Map of the index of the operation to the _id of the inserted document.
  55. ///
  56. id_map inserted_ids() const;
  57. private:
  58. friend collection;
  59. // Construct _inserted_ids from _inserted_ids_owned
  60. MONGOCXX_PRIVATE void _buildInsertedIds();
  61. result::bulk_write _result;
  62. // Array containing documents with the values of the _id field for the inserted documents. This
  63. // array is in the following format: [{"_id": ...}, {"_id": ...}, ...].
  64. bsoncxx::array::value _inserted_ids_owned;
  65. // Points into _inserted_ids_owned.
  66. id_map _inserted_ids;
  67. friend MONGOCXX_API bool MONGOCXX_CALL operator==(const insert_many&, const insert_many&);
  68. friend MONGOCXX_API bool MONGOCXX_CALL operator!=(const insert_many&, const insert_many&);
  69. };
  70. } // namespace result
  71. MONGOCXX_INLINE_NAMESPACE_END
  72. } // namespace mongocxx
  73. #include <mongocxx/config/postlude.hpp>