MongoDB C++ Driver mongocxx-3.1.1
Loading...
Searching...
No Matches
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
53namespace mongocxx {
54MONGOCXX_INLINE_NAMESPACE_BEGIN
55
56class client;
57class database;
58class pipeline;
59
74class MONGOCXX_API collection {
75 public:
82 collection() noexcept;
83
88
92 collection& operator=(collection&&) noexcept;
93
98
102 collection& operator=(const collection&);
103
108
113 explicit operator bool() const noexcept;
114
135 cursor aggregate(const pipeline& pipeline,
136 const options::aggregate& options = options::aggregate());
137
153 //
159 template <typename container_type>
160 MONGOCXX_INLINE stdx::optional<result::bulk_write> bulk_write(
161 const container_type& writes, const options::bulk_write& options = options::bulk_write());
162
185 template <typename write_model_iterator_type>
186 MONGOCXX_INLINE stdx::optional<result::bulk_write> bulk_write(
187 write_model_iterator_type begin, write_model_iterator_type end,
188 const options::bulk_write& options = options::bulk_write());
189
202 stdx::optional<result::bulk_write> bulk_write(const class bulk_write& bulk_write);
203
218 std::int64_t count(bsoncxx::document::view_or_value filter,
219 const options::count& options = options::count());
220
239 bsoncxx::document::value create_index(bsoncxx::document::view_or_value keys,
240 const options::index& options = options::index());
241
258 stdx::optional<result::delete_result> delete_many(
259 bsoncxx::document::view_or_value filter,
260 const options::delete_options& options = options::delete_options());
261
278 stdx::optional<result::delete_result> delete_one(
279 bsoncxx::document::view_or_value filter,
280 const options::delete_options& options = options::delete_options());
281
291
295
298 cursor distinct(bsoncxx::string::view_or_value name, bsoncxx::document::view_or_value filter,
299 const options::distinct& options = options::distinct());
300
312 void drop();
313
331 cursor find(bsoncxx::document::view_or_value filter,
332 const options::find& options = options::find());
333
348 stdx::optional<bsoncxx::document::value> find_one(
349 bsoncxx::document::view_or_value filter, const options::find& options = options::find());
350
367 stdx::optional<bsoncxx::document::value> find_one_and_delete(
368 bsoncxx::document::view_or_value filter,
369 const options::find_one_and_delete& options = options::find_one_and_delete());
370
390 stdx::optional<bsoncxx::document::value> find_one_and_replace(
391 bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value replacement,
392 const options::find_one_and_replace& options = options::find_one_and_replace());
393
413 stdx::optional<bsoncxx::document::value> find_one_and_update(
414 bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value update,
415 const options::find_one_and_update& options = options::find_one_and_update());
416
432 stdx::optional<result::insert_one> insert_one(
433 bsoncxx::document::view_or_value document,
434 const options::insert& options = options::insert());
435
443 //
459 template <typename container_type>
460 MONGOCXX_INLINE stdx::optional<result::insert_many> insert_many(
461 const container_type& container, const options::insert& options = options::insert());
462
487 template <typename document_view_iterator_type>
488 MONGOCXX_INLINE stdx::optional<result::insert_many> insert_many(
489 document_view_iterator_type begin, document_view_iterator_type end,
490 const options::insert& options = options::insert());
491
501 cursor list_indexes() const;
502
508 stdx::string_view name() const;
509
526 void rename(bsoncxx::string::view_or_value new_name, bool drop_target_before_rename = false);
527
538
548
559
568
589 stdx::optional<result::replace_one> replace_one(
590 bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value replacement,
591 const options::update& options = options::update());
592
613 stdx::optional<result::update> update_many(bsoncxx::document::view_or_value filter,
614 bsoncxx::document::view_or_value update,
615 const options::update& options = options::update());
616
637 stdx::optional<result::update> update_one(bsoncxx::document::view_or_value filter,
638 bsoncxx::document::view_or_value update,
639 const options::update& options = options::update());
640
649
656
657 private:
658 friend class database;
659
660 MONGOCXX_PRIVATE collection(const database& database,
661 bsoncxx::string::view_or_value collection_name);
662
663 MONGOCXX_PRIVATE collection(const database& database, void* collection);
664
665 class MONGOCXX_PRIVATE impl;
666
667 MONGOCXX_PRIVATE impl& _get_impl();
668 MONGOCXX_PRIVATE const impl& _get_impl() const;
669
670 std::unique_ptr<impl> _impl;
671};
672
673template <typename container_type>
674MONGOCXX_INLINE stdx::optional<result::bulk_write> collection::bulk_write(
675 const container_type& requests, const options::bulk_write& options) {
676 return bulk_write(requests.begin(), requests.end(), options);
677}
678
679template <typename write_model_iterator_type>
680MONGOCXX_INLINE stdx::optional<result::bulk_write> collection::bulk_write(
681 write_model_iterator_type begin, write_model_iterator_type end,
682 const options::bulk_write& options) {
683 class bulk_write writes(options);
684
685 std::for_each(begin, end, [&](const model::write& current) { writes.append(current); });
686
687 return bulk_write(writes);
688}
689
690template <typename container_type>
691MONGOCXX_INLINE stdx::optional<result::insert_many> collection::insert_many(
692 const container_type& container, const options::insert& options) {
693 return insert_many(container.begin(), container.end(), options);
694}
695
696template <typename document_view_iterator_type>
697MONGOCXX_INLINE stdx::optional<result::insert_many> collection::insert_many(
698 document_view_iterator_type begin, document_view_iterator_type end,
699 const options::insert& options) {
700 auto op = std::for_each(begin, end, insert_many_builder{options});
701
702 return op.insert(this);
703}
704
705MONGOCXX_INLINE_NAMESPACE_END
706} // namespace mongocxx
707
708#include <mongocxx/config/postlude.hpp>
Class representing a batch of write operations that can be sent to the server as a group.
Definition bulk_write.hpp:41
void append(const model::write &operation)
Appends a single write to the bulk write operation.
Class representing server side document groupings within a MongoDB database.
Definition collection.hpp:74
collection() noexcept
Default constructs a collection object.
Class representing a pointer to the result set of a query on a MongoDB server.
Definition cursor.hpp:35
Class representing a MongoDB database.
Definition database.hpp:44
Class to build an insert_many bulk write operation.
Definition insert_many_builder.hpp:34
Models a single write operation within a @bulk_write.
Definition write.hpp:37
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 aggregation pipeline.
Definition pipeline.hpp:35
A class to represent the read concern.
Definition read_concern.hpp:52
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 server-side requirement for reporting the success of a write operation.
Definition write_concern.hpp:54
Top level namespace for MongoDB C++ BSON functionality.
Definition element.hpp:24
Top level namespace for the MongoDB C++ driver.
Definition bulk_write.hpp:22