MongoDB C++ Driver  mongocxx-3.0.2
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/stream/document.hpp>
23 #include <bsoncxx/document/view_or_value.hpp>
24 #include <bsoncxx/stdx/optional.hpp>
25 #include <bsoncxx/string/view_or_value.hpp>
26 #include <mongocxx/bulk_write.hpp>
27 #include <mongocxx/cursor.hpp>
28 #include <mongocxx/insert_many_builder.hpp>
29 #include <mongocxx/options/aggregate.hpp>
30 #include <mongocxx/options/bulk_write.hpp>
31 #include <mongocxx/options/count.hpp>
32 #include <mongocxx/options/delete.hpp>
33 #include <mongocxx/options/distinct.hpp>
34 #include <mongocxx/options/find.hpp>
35 #include <mongocxx/options/find_one_and_delete.hpp>
36 #include <mongocxx/options/find_one_and_replace.hpp>
37 #include <mongocxx/options/find_one_and_update.hpp>
38 #include <mongocxx/options/index.hpp>
39 #include <mongocxx/options/insert.hpp>
40 #include <mongocxx/options/update.hpp>
41 #include <mongocxx/read_concern.hpp>
42 #include <mongocxx/read_preference.hpp>
43 #include <mongocxx/result/bulk_write.hpp>
44 #include <mongocxx/result/delete.hpp>
45 #include <mongocxx/result/insert_many.hpp>
46 #include <mongocxx/result/insert_one.hpp>
47 #include <mongocxx/result/replace_one.hpp>
48 #include <mongocxx/result/update.hpp>
49 #include <mongocxx/write_concern.hpp>
50 
51 #include <mongocxx/config/prelude.hpp>
52 
53 namespace mongocxx {
54 MONGOCXX_INLINE_NAMESPACE_BEGIN
55 
56 class client;
57 class database;
58 class pipeline;
59 
74 class MONGOCXX_API collection {
75  public:
82  collection() noexcept;
83 
87  collection(collection&&) noexcept;
88 
92  collection& operator=(collection&&) noexcept;
93 
97  collection(const collection&);
98 
102  collection& operator=(const collection&);
103 
107  ~collection();
108 
113  explicit operator bool() const noexcept;
114 
129  cursor aggregate(const pipeline& pipeline,
130  const options::aggregate& options = options::aggregate());
131 
145  //
151  template <typename container_type>
152  MONGOCXX_INLINE stdx::optional<result::bulk_write> bulk_write(
153  const container_type& writes, const options::bulk_write& options = options::bulk_write());
154 
177  template <typename write_model_iterator_type>
178  MONGOCXX_INLINE stdx::optional<result::bulk_write> bulk_write(
179  write_model_iterator_type begin, write_model_iterator_type end,
180  const options::bulk_write& options = options::bulk_write());
181 
194  stdx::optional<result::bulk_write> bulk_write(const class bulk_write& bulk_write);
195 
210  std::int64_t count(bsoncxx::document::view_or_value filter,
211  const options::count& options = options::count());
212 
227  const options::index& options = options::index());
228 
243  stdx::optional<result::delete_result> delete_many(
246 
261  stdx::optional<result::delete_result> delete_one(
264 
274 
278 
282  const options::distinct& options = options::distinct());
283 
290  void drop();
291 
309  const options::find& options = options::find());
310 
325  stdx::optional<bsoncxx::document::value> find_one(
327 
340  stdx::optional<bsoncxx::document::value> find_one_and_delete(
343 
363  stdx::optional<bsoncxx::document::value> find_one_and_replace(
366 
386  stdx::optional<bsoncxx::document::value> find_one_and_update(
389 
403  stdx::optional<result::insert_one> insert_one(
405  const options::insert& options = options::insert());
406 
414  //
428  template <typename container_type>
429  MONGOCXX_INLINE stdx::optional<result::insert_many> insert_many(
430  const container_type& container, const options::insert& options = options::insert());
431 
456  template <typename document_view_iterator_type>
457  MONGOCXX_INLINE stdx::optional<result::insert_many> insert_many(
458  document_view_iterator_type begin, document_view_iterator_type end,
459  const options::insert& options = options::insert());
460 
470  cursor list_indexes() const;
471 
477  stdx::string_view name() const;
478 
490  void rename(bsoncxx::string::view_or_value new_name, bool drop_target_before_rename = false);
491 
501  void read_concern(class read_concern rc);
502 
511  class read_concern read_concern() const;
512 
522  void read_preference(class read_preference rp);
523 
531  class read_preference read_preference() const;
532 
549  stdx::optional<result::replace_one> replace_one(
551  const options::update& options = options::update());
552 
569  stdx::optional<result::update> update_many(bsoncxx::document::view_or_value filter,
571  const options::update& options = options::update());
572 
589  stdx::optional<result::update> update_one(bsoncxx::document::view_or_value filter,
591  const options::update& options = options::update());
592 
600  void write_concern(class write_concern wc);
601 
607  class write_concern write_concern() const;
608 
609  private:
610  friend class database;
611 
612  MONGOCXX_PRIVATE collection(const database& database,
613  bsoncxx::string::view_or_value collection_name);
614 
615  MONGOCXX_PRIVATE collection(const database& database, void* collection);
616 
617  class MONGOCXX_PRIVATE impl;
618 
619  MONGOCXX_PRIVATE impl& _get_impl();
620  MONGOCXX_PRIVATE const impl& _get_impl() const;
621 
622  std::unique_ptr<impl> _impl;
623 };
624 
625 template <typename container_type>
626 MONGOCXX_INLINE stdx::optional<result::bulk_write> collection::bulk_write(
627  const container_type& requests, const options::bulk_write& options) {
628  return bulk_write(requests.begin(), requests.end(), options);
629 }
630 
631 template <typename write_model_iterator_type>
632 MONGOCXX_INLINE stdx::optional<result::bulk_write> collection::bulk_write(
633  write_model_iterator_type begin, write_model_iterator_type end,
634  const options::bulk_write& options) {
635  class bulk_write writes(options);
636 
637  std::for_each(begin, end, [&](const model::write& current) { writes.append(current); });
638 
639  return bulk_write(writes);
640 }
641 
642 template <typename container_type>
643 MONGOCXX_INLINE stdx::optional<result::insert_many> collection::insert_many(
644  const container_type& container, const options::insert& options) {
645  return insert_many(container.begin(), container.end(), options);
646 }
647 
648 template <typename document_view_iterator_type>
649 MONGOCXX_INLINE stdx::optional<result::insert_many> collection::insert_many(
650  document_view_iterator_type begin, document_view_iterator_type end,
651  const options::insert& options) {
652  auto op = std::for_each(begin, end, insert_many_builder{options});
653 
654  return op.insert(this);
655 }
656 
657 MONGOCXX_INLINE_NAMESPACE_END
658 } // namespace mongocxx
659 
660 #include <mongocxx/config/postlude.hpp>
void append(const model::write &operation)
Appends a single write to the bulk write operation.
Definition: bulk_write.hpp:22
Class to build an insert_many bulk write operation.
Definition: insert_many_builder.hpp:33
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
Class representing a pointer to the result set of a query on a MongoDB server.
Definition: cursor.hpp:35
Class representing the optional arguments to a MongoDB distinct command.
Definition: distinct.hpp:34
stdx::optional< result::bulk_write > bulk_write(const container_type &writes, const options::bulk_write &options=options::bulk_write())
Sends a container of writes to the server as a bulk write operation.
Definition: collection.hpp:626
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:43
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
Class representing a MongoDB aggregation pipeline.
Definition: pipeline.hpp:34
Class representing a preference for how the driver routes read operations to members of a replica set...
Definition: read_preference.hpp:54
Class representing the optional arguments to a MongoDB aggregation operation.
Definition: aggregate.hpp:34
Class representing a view-or-value variant type for strings.
Definition: view_or_value.hpp:36
Class representing the optional arguments to a MongoDB create index operation.
Definition: index.hpp:39
Models a single write operation within a .
Definition: write.hpp:37
A class to represent the read concern.
Definition: read_concern.hpp:46
Class representing the optional arguments to a MongoDB find_and_modify replace operation.
Definition: find_one_and_replace.hpp:34
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:643
Class representing the optional arguments to a MongoDB find_and_modify delete operation.
Definition: find_one_and_delete.hpp:33
Class representing the server-side requirement for reporting the success of a write operation...
Definition: write_concern.hpp:54
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:34
Class representing server side document groupings within a MongoDB database.
Definition: collection.hpp:74