Mangrove
The C++ Object Document Mapper for MongoDB
collection_wrapper.hpp
1 // Copyright 2016 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 <mangrove/config/prelude.hpp>
18 
19 #include <iostream>
20 
21 #include <bsoncxx/builder/basic/document.hpp>
22 #include <bsoncxx/stdx/optional.hpp>
23 #include <mongocxx/collection.hpp>
24 
25 #include <boson/mapping_functions.hpp>
26 #include <mangrove/deserializing_cursor.hpp>
27 
28 namespace mangrove {
29 MANGROVE_INLINE_NAMESPACE_BEGIN
30 
31 template <class T>
33  public:
34  collection_wrapper() noexcept = default;
35 
36  collection_wrapper(const mongocxx::collection& c) noexcept : _coll(c) {
37  }
38 
39  collection_wrapper(mongocxx::collection&& c) noexcept : _coll(c) {
40  }
41 
42  collection_wrapper(collection_wrapper&&) noexcept = default;
43  collection_wrapper& operator=(collection_wrapper&&) noexcept = default;
44  ~collection_wrapper() = default;
45 
50  mongocxx::collection collection() {
51  return _coll;
52  }
53 
73  template <class Result = T>
75  const mongocxx::pipeline& pipeline,
76  const mongocxx::options::aggregate& options = mongocxx::options::aggregate()) {
77  return deserializing_cursor<Result>(_coll.aggregate(pipeline, options));
78  }
79 
96  bsoncxx::document::view_or_value filter,
97  const mongocxx::options::find& options = mongocxx::options::find()) {
98  return deserializing_cursor<T>(_coll.find(filter, options));
99  }
100 
114  mongocxx::stdx::optional<T> find_one(
115  bsoncxx::document::view_or_value filter,
116  const mongocxx::options::find& options = mongocxx::options::find()) {
117  return boson::to_optional_obj<T>(_coll.find_one(filter, options));
118  }
119 
132  mongocxx::stdx::optional<T> find_one_and_delete(
133  bsoncxx::document::view_or_value filter,
134  const mongocxx::options::find_one_and_delete& options =
135  mongocxx::options::find_one_and_delete()) {
136  return boson::to_optional_obj<T>(_coll.find_one_and_delete(filter, options));
137  }
138 
158  mongocxx::stdx::optional<T> find_one_and_replace(
159  bsoncxx::document::view_or_value filter, const T& replacement,
160  const mongocxx::options::find_one_and_replace& options =
161  mongocxx::options::find_one_and_replace()) {
162  return boson::to_optional_obj<T>(
163  _coll.find_one_and_replace(filter, boson::to_document(replacement), options));
164  }
165 
180  mongocxx::stdx::optional<mongocxx::result::insert_one> insert_one(
181  T obj, const mongocxx::options::insert& options = mongocxx::options::insert()) {
182  return _coll.insert_one(boson::to_document(obj), options);
183  }
184 
195  //
207  template <typename container_type>
208  mongocxx::stdx::optional<mongocxx::result::insert_many> insert_many(
209  const container_type& container,
210  const mongocxx::options::insert& options = mongocxx::options::insert()) {
211  return insert_many(container.begin(), container.end(), options);
212  }
213 
241  template <typename object_iterator_type>
242  mongocxx::stdx::optional<mongocxx::result::insert_many> insert_many(
243  object_iterator_type begin, object_iterator_type end,
244  const mongocxx::options::insert& options = mongocxx::options::insert()) {
245  return _coll.insert_many(boson::serializing_iterator<object_iterator_type>(begin),
247  }
248 
264  mongocxx::stdx::optional<mongocxx::result::replace_one> replace_one(
265  bsoncxx::document::view_or_value filter, const T& replacement,
266  const mongocxx::options::update& options = mongocxx::options::update()) {
267  return _coll.replace_one(filter, boson::to_document(replacement), options);
268  }
269 
270  private:
271  mongocxx::collection _coll;
272 };
273 
274 MANGROVE_INLINE_NAMESPACE_END
275 } // namespace mangrove
276 
277 #include <mangrove/config/postlude.hpp>
mongocxx::stdx::optional< T > find_one_and_replace(bsoncxx::document::view_or_value filter, const T &replacement, const mongocxx::options::find_one_and_replace &options=mongocxx::options::find_one_and_replace())
Finds a single document matching the filter, replaces it, and returns either the original or the repl...
Definition: collection_wrapper.hpp:158
mongocxx::stdx::optional< mongocxx::result::insert_many > insert_many(const container_type &container, const mongocxx::options::insert &options=mongocxx::options::insert())
Inserts multiple serializable objects into the collection.
Definition: collection_wrapper.hpp:208
mongocxx::stdx::optional< mongocxx::result::replace_one > replace_one(bsoncxx::document::view_or_value filter, const T &replacement, const mongocxx::options::update &options=mongocxx::options::update())
Replaces a single document matching the provided filter in this collection.
Definition: collection_wrapper.hpp:264
A class that wraps a mongocxx::cursor.
Definition: deserializing_cursor.hpp:37
An iterator that wraps another iterator of serializable objects, and yields BSON document views corre...
Definition: mapping_functions.hpp:129
mongocxx::stdx::optional< T > find_one(bsoncxx::document::view_or_value filter, const mongocxx::options::find &options=mongocxx::options::find())
Finds a single document in this collection that match the provided filter.
Definition: collection_wrapper.hpp:114
mongocxx::stdx::optional< T > find_one_and_delete(bsoncxx::document::view_or_value filter, const mongocxx::options::find_one_and_delete &options=mongocxx::options::find_one_and_delete())
Finds a single document matching the filter, deletes it, and returns the original as a deserialized o...
Definition: collection_wrapper.hpp:132
mongocxx::stdx::optional< mongocxx::result::insert_many > insert_many(object_iterator_type begin, object_iterator_type end, const mongocxx::options::insert &options=mongocxx::options::insert())
Inserts multiple serializable objects into the collection.
Definition: collection_wrapper.hpp:242
Definition: collection_wrapper.hpp:28
Definition: collection_wrapper.hpp:32
deserializing_cursor< Result > aggregate(const mongocxx::pipeline &pipeline, const mongocxx::options::aggregate &options=mongocxx::options::aggregate())
Runs an aggregation framework pipeline against this collection, and returns the results as de-seriali...
Definition: collection_wrapper.hpp:74
deserializing_cursor< T > find(bsoncxx::document::view_or_value filter, const mongocxx::options::find &options=mongocxx::options::find())
Finds the documents in this collection which match the provided filter.
Definition: collection_wrapper.hpp:95
mongocxx::stdx::optional< mongocxx::result::insert_one > insert_one(T obj, const mongocxx::options::insert &options=mongocxx::options::insert())
Inserts a single serializable object into the collection.
Definition: collection_wrapper.hpp:180
mongocxx::collection collection()
Returns a copy of the underlying collection.
Definition: collection_wrapper.hpp:50