MongoDB C++ Driver  mongocxx-3.7.0
database.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 <memory>
18 #include <string>
19 
20 #include <bsoncxx/document/view_or_value.hpp>
21 #include <bsoncxx/string/view_or_value.hpp>
22 #include <mongocxx/client_session.hpp>
23 #include <mongocxx/collection.hpp>
24 #include <mongocxx/gridfs/bucket.hpp>
25 #include <mongocxx/options/create_collection.hpp>
26 #include <mongocxx/options/gridfs/bucket.hpp>
27 #include <mongocxx/read_preference.hpp>
28 #include <mongocxx/write_concern.hpp>
29 
30 #include <mongocxx/config/prelude.hpp>
31 
32 namespace mongocxx {
33 MONGOCXX_INLINE_NAMESPACE_BEGIN
34 
35 class client;
36 
43 class MONGOCXX_API database {
44  public:
50  database() noexcept;
51 
55  database(database&&) noexcept;
56 
60  database& operator=(database&&) noexcept;
61 
65  database(const database&);
66 
70  database& operator=(const database&);
71 
75  ~database();
76 
81  explicit operator bool() const noexcept;
82 
106  cursor aggregate(const pipeline& pipeline,
107  const options::aggregate& options = options::aggregate());
108 
132  cursor aggregate(const client_session& session,
133  const pipeline& pipeline,
134  const options::aggregate& options = options::aggregate());
138 
152 
164  bsoncxx::document::value run_command(const client_session& session,
166 
179  uint32_t server_id);
183 
206  class collection create_collection(stdx::string_view name,
207  bsoncxx::document::view_or_value collection_options = {},
208  const stdx::optional<write_concern>& write_concern = {});
209 
232  class collection create_collection(const client_session& session,
233  stdx::string_view name,
234  bsoncxx::document::view_or_value collection_options = {},
235  const stdx::optional<write_concern>& write_concern = {});
236 
258  MONGOCXX_DEPRECATED class collection create_collection(
259  bsoncxx::string::view_or_value name,
260  const options::create_collection_deprecated& collection_options,
261  const stdx::optional<write_concern>& write_concern = {}) {
262  return create_collection_deprecated(name, collection_options, write_concern);
263  }
264 
265  class collection create_collection_deprecated(
266  bsoncxx::string::view_or_value name,
267  const options::create_collection_deprecated& collection_options,
268  const stdx::optional<write_concern>& write_concern = {});
269 
293  MONGOCXX_DEPRECATED class collection create_collection(
294  const client_session& session,
296  const options::create_collection_deprecated& collection_options,
297  const stdx::optional<write_concern>& write_concern = {}) {
298  return create_collection_deprecated(session, name, collection_options, write_concern);
299  }
300 
324  class collection create_collection_deprecated(
325  const client_session& session,
327  const options::create_collection_deprecated& collection_options,
328  const stdx::optional<write_concern>& write_concern = {});
329 
333 
349  void drop(const bsoncxx::stdx::optional<mongocxx::write_concern>& write_concern = {});
350 
366  void drop(const client_session& session,
367  const bsoncxx::stdx::optional<mongocxx::write_concern>& write_concern = {});
371 
382  bool has_collection(bsoncxx::string::view_or_value name) const;
383 
396  cursor list_collections(bsoncxx::document::view_or_value filter = {});
397 
410  cursor list_collections(const client_session& session,
412 
426  std::vector<std::string> list_collection_names(bsoncxx::document::view_or_value filter = {});
427 
443  std::vector<std::string> list_collection_names(const client_session& session,
445 
449 
455  stdx::string_view name() const;
456 
469  void read_concern(class read_concern rc);
470 
479  class read_concern read_concern() const;
480 
492  void read_preference(class read_preference rp);
493 
501  class read_preference read_preference() const;
502 
510  void write_concern(class write_concern wc);
511 
517  class write_concern write_concern() const;
518 
526  class collection collection(bsoncxx::string::view_or_value name) const;
527 
536  MONGOCXX_INLINE class collection operator[](bsoncxx::string::view_or_value name) const;
537 
552  class gridfs::bucket gridfs_bucket(
553  const options::gridfs::bucket& options = options::gridfs::bucket()) const;
554 
569  change_stream watch(const options::change_stream& options = {});
570 
582  change_stream watch(const client_session& session, const options::change_stream& options = {});
583 
600  change_stream watch(const pipeline& pipe, const options::change_stream& options = {});
601 
617  change_stream watch(const client_session& session,
618  const pipeline& pipe,
619  const options::change_stream& options = {});
620 
624 
625  private:
626  friend class client;
627  friend class collection;
628 
629  MONGOCXX_PRIVATE database(const class client& client, bsoncxx::string::view_or_value name);
630 
631  MONGOCXX_PRIVATE cursor _aggregate(const client_session* session,
632  const pipeline& pipeline,
633  const options::aggregate& options);
634 
635  MONGOCXX_PRIVATE bsoncxx::document::value _run_command(
636  const client_session* session, bsoncxx::document::view_or_value command);
637 
638  MONGOCXX_PRIVATE class collection _create_collection(
639  const client_session* session,
640  stdx::string_view name,
641  bsoncxx::document::view_or_value collection_options,
642  const stdx::optional<class write_concern>& write_concern);
643 
644  MONGOCXX_PRIVATE class collection _create_collection_deprecated(
645  const client_session* session,
646  bsoncxx::string::view_or_value name,
647  const options::create_collection_deprecated& collection_options,
648  const stdx::optional<class write_concern>& write_concern);
649 
650  MONGOCXX_PRIVATE cursor _list_collections(const client_session* session,
651  bsoncxx::document::view_or_value filter);
652 
653  MONGOCXX_PRIVATE std::vector<std::string> _list_collection_names(
654  const client_session* session, bsoncxx::document::view_or_value filter);
655 
656  MONGOCXX_PRIVATE void _drop(
657  const client_session* session,
658  const bsoncxx::stdx::optional<mongocxx::write_concern>& write_concern);
659 
660  MONGOCXX_PRIVATE change_stream _watch(const client_session* session,
661  const pipeline& pipe,
662  const options::change_stream& options);
663 
664  class MONGOCXX_PRIVATE impl;
665 
666  MONGOCXX_PRIVATE impl& _get_impl();
667  MONGOCXX_PRIVATE const impl& _get_impl() const;
668 
669  std::unique_ptr<impl> _impl;
670 };
671 
672 MONGOCXX_INLINE collection database::operator[](bsoncxx::string::view_or_value name) const {
673  return collection(name);
674 }
675 
676 MONGOCXX_INLINE_NAMESPACE_END
677 } // namespace mongocxx
678 
679 #include <mongocxx/config/postlude.hpp>
mongocxx
Top level namespace for the MongoDB C++ driver.
Definition: bulk_write.hpp:24
bsoncxx
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
bsoncxx::view_or_value< document::view, document::value >
mongocxx::options::create_collection_deprecated
Class representing the optional arguments to a MongoDB createCollection command.
Definition: create_collection.hpp:35
mongocxx::options::aggregate
Class representing the optional arguments to a MongoDB aggregation operation.
Definition: aggregate.hpp:39
mongocxx::write_concern
Class representing the server-side requirement for reporting the success of a write operation.
Definition: write_concern.hpp:56
bsoncxx::document::value
A read-only BSON document that owns its underlying buffer.
Definition: value.hpp:34
bsoncxx::string::view_or_value
Class representing a view-or-value variant type for strings.
Definition: view_or_value.hpp:36
mongocxx::pipeline
Class representing a MongoDB aggregation pipeline.
Definition: pipeline.hpp:38
mongocxx::client_session
Use a session for a sequence of operations, optionally with either causal consistency or snapshots.
Definition: client_session.hpp:40
mongocxx::database
Class representing a MongoDB database.
Definition: database.hpp:43
mongocxx::collection
Class representing server side document groupings within a MongoDB database.
Definition: collection.hpp:83
mongocxx::cursor
Class representing a pointer to the result set of a query on a MongoDB server.
Definition: cursor.hpp:36