MongoDB C++ Driver  mongocxx-3.2.0
collection.hpp
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 
15 #pragma once
16 
17 #include <algorithm>
18 #include <cstdint>
19 #include <memory>
20 #include <string>
21 
22 #include <bsoncxx/builder/basic/array.hpp>
23 #include <bsoncxx/builder/basic/document.hpp>
24 #include <bsoncxx/builder/basic/kvp.hpp>
25 #include <bsoncxx/builder/concatenate.hpp>
26 #include <bsoncxx/document/view_or_value.hpp>
27 #include <bsoncxx/oid.hpp>
28 #include <bsoncxx/stdx/optional.hpp>
29 #include <bsoncxx/string/view_or_value.hpp>
30 #include <mongocxx/bulk_write.hpp>
31 #include <mongocxx/cursor.hpp>
32 #include <mongocxx/index_view.hpp>
33 #include <mongocxx/model/insert_one.hpp>
34 #include <mongocxx/options/aggregate.hpp>
35 #include <mongocxx/options/bulk_write.hpp>
36 #include <mongocxx/options/count.hpp>
37 #include <mongocxx/options/delete.hpp>
38 #include <mongocxx/options/distinct.hpp>
39 #include <mongocxx/options/find.hpp>
40 #include <mongocxx/options/find_one_and_delete.hpp>
41 #include <mongocxx/options/find_one_and_replace.hpp>
42 #include <mongocxx/options/find_one_and_update.hpp>
43 #include <mongocxx/options/index.hpp>
44 #include <mongocxx/options/index_view.hpp>
45 #include <mongocxx/options/insert.hpp>
46 #include <mongocxx/options/update.hpp>
47 #include <mongocxx/read_concern.hpp>
48 #include <mongocxx/read_preference.hpp>
49 #include <mongocxx/result/bulk_write.hpp>
50 #include <mongocxx/result/delete.hpp>
51 #include <mongocxx/result/insert_many.hpp>
52 #include <mongocxx/result/insert_one.hpp>
53 #include <mongocxx/result/replace_one.hpp>
54 #include <mongocxx/result/update.hpp>
55 #include <mongocxx/write_concern.hpp>
56 
57 #include <mongocxx/config/prelude.hpp>
58 
59 namespace mongocxx {
60 MONGOCXX_INLINE_NAMESPACE_BEGIN
61 
62 using bsoncxx::builder::basic::kvp;
63 using bsoncxx::builder::basic::make_document;
64 using bsoncxx::builder::concatenate;
65 
66 class client;
67 class database;
68 class pipeline;
69 
84 class MONGOCXX_API collection {
85  public:
92  collection() noexcept;
93 
97  collection(collection&&) noexcept;
98 
102  collection& operator=(collection&&) noexcept;
103 
107  collection(const collection&);
108 
112  collection& operator=(const collection&);
113 
117  ~collection();
118 
123  explicit operator bool() const noexcept;
124 
144  cursor aggregate(const pipeline& pipeline,
145  const options::aggregate& options = options::aggregate());
146 
156  class bulk_write create_bulk_write(const options::bulk_write& options = {});
157 
170  //
178  MONGOCXX_INLINE stdx::optional<result::bulk_write> write(
179  const model::write& write, const options::bulk_write& options = options::bulk_write());
180 
196  //
202  template <typename container_type>
203  MONGOCXX_INLINE stdx::optional<result::bulk_write> bulk_write(
204  const container_type& writes, const options::bulk_write& options = options::bulk_write());
205 
228  template <typename write_model_iterator_type>
229  MONGOCXX_INLINE stdx::optional<result::bulk_write> bulk_write(
230  write_model_iterator_type begin,
231  write_model_iterator_type end,
232  const options::bulk_write& options = options::bulk_write());
233 
246  stdx::optional<result::bulk_write> bulk_write(const class bulk_write& bulk_write);
247 
262  std::int64_t count(bsoncxx::document::view_or_value filter,
263  const options::count& options = options::count());
264 
284  bsoncxx::document::value create_index(
286  bsoncxx::document::view_or_value index_options = {},
287  options::index_view operation_options = options::index_view{});
288 
305  stdx::optional<result::delete_result> delete_many(
308 
325  stdx::optional<result::delete_result> delete_one(
328 
338 
342 
347  const options::distinct& options = options::distinct());
348 
364  void drop(const bsoncxx::stdx::optional<mongocxx::write_concern>& write_concern = {});
365 
384  const options::find& options = options::find());
385 
400  stdx::optional<bsoncxx::document::value> find_one(
402 
420  stdx::optional<bsoncxx::document::value> find_one_and_delete(
423 
444  stdx::optional<bsoncxx::document::value> find_one_and_replace(
448 
469  stdx::optional<bsoncxx::document::value> find_one_and_update(
473 
489  stdx::optional<result::insert_one> insert_one(
491  const options::insert& options = options::insert());
492 
500  //
516  template <typename container_type>
517  MONGOCXX_INLINE stdx::optional<result::insert_many> insert_many(
518  const container_type& container, const options::insert& options = options::insert());
519 
544  template <typename document_view_iterator_type>
545  MONGOCXX_INLINE stdx::optional<result::insert_many> insert_many(
546  document_view_iterator_type begin,
547  document_view_iterator_type end,
548  const options::insert& options = options::insert());
549 
559  cursor list_indexes() const;
560 
567  stdx::string_view name() const;
568 
585  void rename(bsoncxx::string::view_or_value new_name,
586  bool drop_target_before_rename = false,
587  const bsoncxx::stdx::optional<write_concern>& write_concern = {});
588 
598  void read_concern(class read_concern rc);
599 
608  class read_concern read_concern() const;
609 
619  void read_preference(class read_preference rp);
620 
628  class read_preference read_preference() const;
629 
650  stdx::optional<result::replace_one> replace_one(
653  const options::update& options = options::update());
654 
675  stdx::optional<result::update> update_many(bsoncxx::document::view_or_value filter,
677  const options::update& options = options::update());
678 
699  stdx::optional<result::update> update_one(bsoncxx::document::view_or_value filter,
701  const options::update& options = options::update());
702 
710  void write_concern(class write_concern wc);
711 
717  class write_concern write_concern() const;
718 
721  index_view indexes();
722 
723  private:
724  friend class bulk_write;
725  friend class database;
726 
727  MONGOCXX_PRIVATE collection(const database& database,
728  bsoncxx::string::view_or_value collection_name);
729 
730  MONGOCXX_PRIVATE collection(const database& database, void* collection);
731 
732  class MONGOCXX_PRIVATE impl;
733 
734  MONGOCXX_PRIVATE impl& _get_impl();
735  MONGOCXX_PRIVATE const impl& _get_impl() const;
736 
737  std::unique_ptr<impl> _impl;
738 };
739 
740 MONGOCXX_INLINE stdx::optional<result::bulk_write> collection::write(
741  const model::write& write, const options::bulk_write& options) {
742  auto writes = create_bulk_write(options);
743  writes.append(write);
744 
745  return bulk_write(writes);
746 }
747 
748 template <typename container_type>
749 MONGOCXX_INLINE stdx::optional<result::bulk_write> collection::bulk_write(
750  const container_type& requests, const options::bulk_write& options) {
751  return bulk_write(requests.begin(), requests.end(), options);
752 }
753 
754 template <typename write_model_iterator_type>
755 MONGOCXX_INLINE stdx::optional<result::bulk_write> collection::bulk_write(
756  write_model_iterator_type begin,
757  write_model_iterator_type end,
758  const options::bulk_write& options) {
759  auto writes = create_bulk_write(options);
760 
761  std::for_each(begin, end, [&](const model::write& current) { writes.append(current); });
762 
763  return bulk_write(writes);
764 }
765 
766 template <typename container_type>
767 MONGOCXX_INLINE stdx::optional<result::insert_many> collection::insert_many(
768  const container_type& container, const options::insert& options) {
769  return insert_many(container.begin(), container.end(), options);
770 }
771 
772 template <typename document_view_iterator_type>
773 MONGOCXX_INLINE stdx::optional<result::insert_many> collection::insert_many(
774  document_view_iterator_type begin,
775  document_view_iterator_type end,
776  const options::insert& options) {
777  options::bulk_write bulk_write_options;
778  bulk_write_options.ordered(options.ordered().value_or(true));
779  if (options.write_concern()) {
780  bulk_write_options.write_concern(*options.write_concern());
781  }
782  if (options.bypass_document_validation()) {
783  bulk_write_options.bypass_document_validation(*options.bypass_document_validation());
784  }
785 
786  auto writes = create_bulk_write(bulk_write_options);
787  bsoncxx::builder::basic::array inserted_ids;
788 
789  std::for_each(begin, end, [&inserted_ids, &writes](const bsoncxx::document::view& doc) {
791 
792  if (!doc["_id"]) {
793  id_doc.append(kvp("_id", bsoncxx::oid{}));
794  writes.append(
795  model::insert_one{make_document(concatenate(id_doc.view()), concatenate(doc))});
796  } else {
797  id_doc.append(kvp("_id", doc["_id"].get_value()));
798  writes.append(model::insert_one{doc});
799  }
800 
801  inserted_ids.append(id_doc.view());
802  });
803 
804  auto result = bulk_write(writes);
805  if (!result) {
806  return stdx::nullopt;
807  }
808 
809  return stdx::optional<result::insert_many>{
810  result::insert_many{std::move(result.value()), inserted_ids.view()}};
811 }
812 
813 MONGOCXX_INLINE_NAMESPACE_END
814 } // namespace mongocxx
815 
816 #include <mongocxx/config/postlude.hpp>
Top level namespace for the MongoDB C++ driver.
Definition: bulk_write.hpp:22
insert & ordered(bool ordered)
Represents a MongoDB ObjectId.
Definition: oid.hpp:38
A read-only BSON document that owns its underlying buffer.
Definition: value.hpp:33
Class representing the optional arguments to a MongoDB query.
Definition: find.hpp:36
bulk_write & write_concern(class write_concern wc)
Sets the write_concern for this operation.
Class representing the result of a MongoDB insert many operation (executed as a bulk write)...
Definition: insert_many.hpp:37
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33
bsoncxx::document::view view() const
Definition: document.hpp:61
Class representing a pointer to the result set of a query on a MongoDB server.
Definition: cursor.hpp:36
bulk_write & ordered(bool ordered)
Sets whether the writes must be executed in order by the server.
Class representing the optional arguments to a MongoDB distinct command.
Definition: distinct.hpp:34
Class representing the optional arguments to a MongoDB bulk write.
Definition: bulk_write.hpp:29
Class representing the optional arguments to a MongoDB insert operation.
Definition: insert.hpp:31
Class representing a MongoDB database.
Definition: database.hpp:46
void append(Arg &&a, Args &&... args)
Appends multiple BSON values.
Definition: sub_array.hpp:50
stdx::optional< result::bulk_write > write(const model::write &write, const options::bulk_write &options=options::bulk_write())
Sends a write to the server as a bulk write operation.
Definition: collection.hpp:740
insert & write_concern(class write_concern wc)
Sets the write_concern for this operation.
Class representing the optional arguments to a MongoDB update operation.
Definition: update.hpp:31
Class representing a batch of write operations that can be sent to the server as a group...
Definition: bulk_write.hpp:41
Class representing the optional arguments to a MongoDB delete operation.
Definition: delete.hpp:30
A traditional builder-style interface for constructing a BSON array.
Definition: array.hpp:35
Class representing a MongoDB aggregation pipeline.
Definition: pipeline.hpp:35
Class representing a preference for how the driver routes read operations to members of a replica set...
Definition: read_preference.hpp:57
Class representing the optional arguments to a MongoDB aggregation operation.
Definition: aggregate.hpp:36
Class representing a view-or-value variant type for strings.
Definition: view_or_value.hpp:36
insert & bypass_document_validation(bool bypass_document_validation)
Sets the bypass_document_validation option.
bsoncxx::array::view view() const
Definition: array.hpp:58
Models a single write operation within a .
Definition: write.hpp:37
Definition: index_view.hpp:31
class bulk_write create_bulk_write(const options::bulk_write &options={})
Creates a new bulk operation to be executed against this collection.
Class representing optional arguments to IndexView operations.
Definition: index_view.hpp:31
void append(Arg &&a, Args &&... args)
Appends multiple basic::kvp key-value pairs.
Definition: sub_document.hpp:46
A class to represent the read concern.
Definition: read_concern.hpp:52
Class representing the optional arguments to a MongoDB find_and_modify replace operation.
Definition: find_one_and_replace.hpp:35
A traditional builder-style interface for constructing a BSON document.
Definition: document.hpp:37
stdx::optional< result::insert_many > insert_many(const container_type &container, const options::insert &options=options::insert())
Inserts multiple documents into the collection.
Definition: collection.hpp:767
Class representing the optional arguments to a MongoDB find_and_modify delete operation.
Definition: find_one_and_delete.hpp:33
bulk_write & bypass_document_validation(bool bypass_document_validation)
Set whether or not to bypass document validation for this operation.
Class representing a MongoDB insert operation that creates a single document.
Definition: insert_one.hpp:28
Class representing the server-side requirement for reporting the success of a write operation...
Definition: write_concern.hpp:55
Class representing the optional arguments to a MongoDB count command.
Definition: count.hpp:36
Class representing the optional arguments to a MongoDB find_and_modify update operation.
Definition: find_one_and_update.hpp:35
Class representing server side document groupings within a MongoDB database.
Definition: collection.hpp:84