1//
2// main.cpp
3//
4// This sample shows SQL to mongo Shell to C++ examples.
5//
6// Copyright (c) 2013, Applied Informatics Software Engineering GmbH.
7// and Contributors.
8//
9// SPDX-License-Identifier: BSL-1.0
10//
11
12
13#include "Poco/MongoDB/MongoDB.h"
14#include "Poco/MongoDB/Connection.h"
15#include "Poco/MongoDB/Database.h"
16#include "Poco/MongoDB/Cursor.h"
17#include "Poco/MongoDB/Array.h"
18
19
20// INSERT INTO players
21// VALUES( "Messi", "Lionel", 1987)
22void sample1(Poco::MongoDB::Connection& connection)
23{
24 std::cout << "*** SAMPLE 1 ***" << std::endl;
25
26 Poco::MongoDB::Database db("sample");
27 Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertPlayerRequest = db.createInsertRequest("players");
28
29 // With one insert request, we can add multiple documents
30 insertPlayerRequest->addNewDocument()
31 .add("lastname", "Valdes")
32 .add("firstname", "Victor")
33 .add("birthyear", 1982);
34 insertPlayerRequest->addNewDocument()
35 .add("lastname", "Alves")
36 .add("firstname", "Daniel")
37 .add("birthyear", 1983);
38 insertPlayerRequest->addNewDocument()
39 .add("lastname", "Bartra")
40 .add("firstname", "Marc")
41 .add("birthyear", 1991);
42 insertPlayerRequest->addNewDocument()
43 .add("lastname", "Alba")
44 .add("firstname", "Jordi")
45 .add("birthyear", 1989);
46 insertPlayerRequest->addNewDocument()
47 .add("lastname", "Montoya")
48 .add("firstname", "Martin")
49 .add("birthyear", 1991);
50 insertPlayerRequest->addNewDocument()
51 .add("lastname", "Abidal")
52 .add("firstname", "Eric")
53 .add("birthyear", 1979);
54 insertPlayerRequest->addNewDocument()
55 .add("lastname", "Fontas")
56 .add("firstname", "Andreu")
57 .add("birthyear", 1989);
58 insertPlayerRequest->addNewDocument()
59 .add("lastname", "Messi")
60 .add("firstname", "Lionel")
61 .add("birthyear", 1987);
62 insertPlayerRequest->addNewDocument()
63 .add("lastname", "Puyol")
64 .add("firstname", "Carles")
65 .add("birthyear", 1978);
66 insertPlayerRequest->addNewDocument()
67 .add("lastname", "Piqué")
68 .add("firstname", "Gerard")
69 .add("birthyear", 1987);
70 insertPlayerRequest->addNewDocument()
71 .add("lastname", "Muniesa")
72 .add("firstname", "Marc")
73 .add("birthyear", 1992);
74 insertPlayerRequest->addNewDocument()
75 .add("lastname", "Fabrégas")
76 .add("firstname", "Cesc")
77 .add("birthyear", 1987);
78 insertPlayerRequest->addNewDocument()
79 .add("lastname", "Hernandez")
80 .add("firstname", "Xavi")
81 .add("birthyear", 1980);
82 insertPlayerRequest->addNewDocument()
83 .add("lastname", "Iniesta")
84 .add("firstname", "Andres")
85 .add("birthyear", 1984);
86 insertPlayerRequest->addNewDocument()
87 .add("lastname", "Alcantara")
88 .add("firstname", "Thiago")
89 .add("birthyear", 1991);
90 insertPlayerRequest->addNewDocument()
91 .add("lastname", "Dos Santos")
92 .add("firstname", "Jonathan")
93 .add("birthyear", 1990);
94 insertPlayerRequest->addNewDocument()
95 .add("lastname", "Mascherano")
96 .add("firstname", "Javier")
97 .add("birthyear", 1984);
98 insertPlayerRequest->addNewDocument()
99 .add("lastname", "Busquets")
100 .add("firstname", "Sergio")
101 .add("birthyear", 1988);
102 insertPlayerRequest->addNewDocument()
103 .add("lastname", "Adriano")
104 .add("firstname", "")
105 .add("birthyear", 1984);
106 insertPlayerRequest->addNewDocument()
107 .add("lastname", "Song")
108 .add("firstname", "Alex")
109 .add("birthyear", 1987);
110 insertPlayerRequest->addNewDocument()
111 .add("lastname", "Villa")
112 .add("firstname", "David")
113 .add("birthyear", 1981);
114 insertPlayerRequest->addNewDocument()
115 .add("lastname", "Sanchez")
116 .add("firstname", "Alexis")
117 .add("birthyear", 1988);
118 insertPlayerRequest->addNewDocument()
119 .add("lastname", "Pedro")
120 .add("firstname", "")
121 .add("birthyear", 1987);
122 insertPlayerRequest->addNewDocument()
123 .add("lastname", "Cuenca")
124 .add("firstname", "Isaac")
125 .add("birthyear", 1991);
126 insertPlayerRequest->addNewDocument()
127 .add("lastname", "Tello")
128 .add("firstname", "Cristian")
129 .add("birthyear", 1991);
130
131 std::cout << insertPlayerRequest->documents().size() << std::endl;
132
133 connection.sendRequest(*insertPlayerRequest);
134 std::string lastError = db.getLastError(connection);
135 if (!lastError.empty())
136 {
137 std::cout << "Last Error: " << db.getLastError(connection) << std::endl;
138 }
139}
140
141
142// SELECT lastname, birthyear FROM players
143void sample2(Poco::MongoDB::Connection& connection)
144{
145 std::cout << "*** SAMPLE 2 ***" << std::endl;
146
147 Poco::MongoDB::Cursor cursor("sample", "players");
148 // Selecting fields is done by adding them to the returnFieldSelector
149 // Use 1 as value of the element.
150 cursor.query().returnFieldSelector().add("lastname", 1);
151 cursor.query().returnFieldSelector().add("birthyear", 1);
152 Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
153 for (;;)
154 {
155 for (Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
156 {
157 std::cout << (*it)->get<std::string>("lastname") << " (" << (*it)->get<int>("birthyear") << ')' << std::endl;
158 }
159
160 // When the cursorID is 0, there are no documents left, so break out ...
161 if (response.cursorID() == 0)
162 {
163 break;
164 }
165
166 // Get the next bunch of documents
167 response = cursor.next(connection);
168 }
169}
170
171
172// SELECT * FROM players
173void sample3(Poco::MongoDB::Connection& connection)
174{
175 std::cout << "*** SAMPLE 3 ***" << std::endl;
176
177 Poco::MongoDB::Cursor cursor("sample", "players");
178 Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
179 for (;;)
180 {
181 for (Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
182 {
183 std::cout << (*it)->get<std::string>("lastname") << ' ' << (*it)->get<std::string>("firstname") << " (" << (*it)->get<int>("birthyear") << ')' << std::endl;
184 }
185
186 // When the cursorID is 0, there are no documents left, so break out ...
187 if (response.cursorID() == 0)
188 {
189 break;
190 }
191
192 // Get the next bunch of documents
193 response = cursor.next(connection);
194 };
195}
196
197
198// SELECT * FROM players WHERE birthyear = 1978
199void sample4(Poco::MongoDB::Connection& connection)
200{
201 std::cout << "*** SAMPLE 4 ***" << std::endl;
202
203 Poco::MongoDB::Cursor cursor("sample", "players");
204 cursor.query().selector().add("birthyear", 1978);
205
206 Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
207 for (;;)
208 {
209 for (Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
210 {
211 std::cout << (*it)->get<std::string>("lastname") << ' ' << (*it)->get<std::string>("firstname") << " (" << (*it)->get<int>("birthyear") << ')' << std::endl;
212 }
213
214 // When the cursorID is 0, there are no documents left, so break out ...
215 if (response.cursorID() == 0)
216 {
217 break;
218 }
219
220 // Get the next bunch of documents
221 response = cursor.next(connection);
222 };
223}
224
225
226// SELECT * FROM players WHERE birthyear = 1987 ORDER BY name
227void sample5(Poco::MongoDB::Connection& connection)
228{
229 std::cout << "*** SAMPLE 5 ***" << std::endl;
230
231 Poco::MongoDB::Cursor cursor("sample", "players");
232
233 // When orderby is needed, use 2 separate documents in the query selector
234 cursor.query().selector().addNewDocument("$query").add("birthyear", 1987);
235 cursor.query().selector().addNewDocument("$orderby").add("lastname", 1);
236
237 Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
238 for (;;)
239 {
240 for (Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
241 {
242 std::cout << (*it)->get<std::string>("lastname") << ' ' << (*it)->get<std::string>("firstname") << " (" << (*it)->get<int>("birthyear") << ')' << std::endl;
243 }
244
245 // When the cursorID is 0, there are no documents left, so break out ...
246 if (response.cursorID() == 0)
247 {
248 break;
249 }
250
251 // Get the next bunch of documents
252 response = cursor.next(connection);
253 };
254}
255
256
257// SELECT * FROM players WHERE birthyear > 1969 and birthyear <= 1980
258void sample6(Poco::MongoDB::Connection& connection)
259{
260 std::cout << "*** SAMPLE 6 ***" << std::endl;
261
262 Poco::MongoDB::Cursor cursor("sample", "players");
263
264 cursor.query().selector().addNewDocument("birthyear")
265 .add("$gt", 1969)
266 .add("$lte", 1980);
267
268 Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
269 for (;;)
270 {
271 for (Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
272 {
273 std::cout << (*it)->get<std::string>("lastname") << ' ' << (*it)->get<std::string>("firstname") << " (" << (*it)->get<int>("birthyear") << ')' << std::endl;
274 }
275
276 // When the cursorID is 0, there are no documents left, so break out ...
277 if (response.cursorID() == 0)
278 {
279 break;
280 }
281
282 // Get the next bunch of documents
283 response = cursor.next(connection);
284 };
285}
286
287
288// CREATE INDEX playername
289// ON players(lastname)
290void sample7(Poco::MongoDB::Connection& connection)
291{
292 std::cout << "*** SAMPLE 7 ***" << std::endl;
293
294 Poco::MongoDB::Database db("sample");
295 Poco::MongoDB::Document::Ptr keys = new Poco::MongoDB::Document();
296 keys->add("lastname", 1);
297 Poco::MongoDB::Document::Ptr errorDoc = db.ensureIndex(connection, "players", "lastname", keys);
298
299 /* Sample above is the same as the following code:
300 Poco::MongoDB::Document::Ptr index = new Poco::MongoDB::Document();
301 index->add("ns", "sample.players");
302 index->add("name", "lastname");
303 index->addNewDocument("key").add("lastname", 1);
304
305 Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest = db.createInsertRequest("system.indexes");
306 insertRequest->documents().push_back(index);
307 connection.sendRequest(*insertRequest);
308 Poco::MongoDB::Document::Ptr errorDoc = db.getLastErrorDoc(connection);
309 */
310 std::cout << errorDoc->toString(2);
311}
312
313
314// SELECT * FROM players LIMIT 10 SKIP 20
315void sample8(Poco::MongoDB::Connection& connection)
316{
317 std::cout << "*** SAMPLE 8 ***" << std::endl;
318
319 Poco::MongoDB::Cursor cursor("sample", "players");
320 cursor.query().setNumberToReturn(10);
321 cursor.query().setNumberToSkip(20);
322 Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
323 for (;;)
324 {
325 for (Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
326 {
327 std::cout << (*it)->get<std::string>("lastname") << ' ' << (*it)->get<std::string>("firstname") << " (" << (*it)->get<int>("birthyear") << ')' << std::endl;
328 }
329
330 // When the cursorID is 0, there are no documents left, so break out ...
331 if (response.cursorID() == 0)
332 {
333 break;
334 }
335
336 // Get the next bunch of documents
337 response = cursor.next(connection);
338 };
339}
340
341// SELECT * FROM players LIMIT 1
342void sample9(Poco::MongoDB::Connection& connection)
343{
344 std::cout << "*** SAMPLE 9 ***" << std::endl;
345
346 // QueryRequest can be used directly
347 Poco::MongoDB::QueryRequest query("sample.players");
348 query.setNumberToReturn(1);
349 Poco::MongoDB::ResponseMessage response;
350 connection.sendRequest(query, response);
351 if (response.hasDocuments())
352 {
353 std::cout << response.documents()[0]->toString(2) << std::endl;
354 }
355
356 // QueryRequest can be created using the Database class
357 Poco::MongoDB::Database db("sample");
358 Poco::SharedPtr<Poco::MongoDB::QueryRequest> queryPtr = db.createQueryRequest("players");
359 queryPtr->setNumberToReturn(1);
360 connection.sendRequest(*queryPtr, response);
361 if (response.hasDocuments())
362 {
363 std::cout << response.documents()[0]->toString(2) << std::endl;
364 }
365}
366
367// SELECT DISTINCT birthyear FROM players WHERE birthyear > 1980
368void sample10(Poco::MongoDB::Connection& connection)
369{
370 std::cout << "*** SAMPLE 10 ***" << std::endl;
371
372 Poco::MongoDB::Database db("sample");
373 Poco::SharedPtr<Poco::MongoDB::QueryRequest> command = db.createCommand();
374
375 command->selector()
376 .add("distinct", "players")
377 .add("key", "birthyear")
378 .addNewDocument("query")
379 .addNewDocument("birthyear")
380 .add("$gt", 1980);
381
382 Poco::MongoDB::ResponseMessage response;
383 connection.sendRequest(*command, response);
384 if (response.hasDocuments())
385 {
386 Poco::MongoDB::Array::Ptr values = response.documents()[0]->get<Poco::MongoDB::Array::Ptr>("values");
387 for (int i = 0; i < values->size(); ++i )
388 {
389 std::cout << values->get<int>(i) << std::endl;
390 }
391 }
392
393}
394
395// SELECT COUNT(*) FROM players WHERE birthyear > 1980
396void sample11(Poco::MongoDB::Connection& connection)
397{
398 std::cout << "*** SAMPLE 11 ***" << std::endl;
399
400 Poco::MongoDB::Database db("sample");
401 Poco::SharedPtr<Poco::MongoDB::QueryRequest> count = db.createCountRequest("players");
402 count->selector().addNewDocument("query")
403 .addNewDocument("birthyear")
404 .add("$gt", 1980);
405
406 Poco::MongoDB::ResponseMessage response;
407 connection.sendRequest(*count, response);
408
409 if (response.hasDocuments())
410 {
411 std::cout << "Count: " << response.documents()[0]->getInteger("n") << std::endl;
412 }
413}
414
415
416//UPDATE players SET birthyear = birthyear + 1 WHERE firstname = 'Victor'
417void sample12(Poco::MongoDB::Connection& connection)
418{
419 std::cout << "*** SAMPLE 12 ***" << std::endl;
420
421 Poco::MongoDB::Database db("sample");
422 Poco::SharedPtr<Poco::MongoDB::UpdateRequest> request = db.createUpdateRequest("players");
423 request->selector().add("firstname", "Victor");
424
425 request->update().addNewDocument("$inc").add("birthyear", 1);
426
427 connection.sendRequest(*request);
428
429 Poco::MongoDB::Document::Ptr lastError = db.getLastErrorDoc(connection);
430 std::cout << "LastError: " << lastError->toString(2) << std::endl;
431}
432
433
434//DELETE players WHERE firstname = 'Victor'
435void sample13(Poco::MongoDB::Connection& connection)
436{
437 std::cout << "*** SAMPLE 13 ***" << std::endl;
438
439 Poco::MongoDB::Database db("sample");
440 Poco::SharedPtr<Poco::MongoDB::DeleteRequest> request = db.createDeleteRequest("players");
441 request->selector().add("firstname", "Victor");
442
443 connection.sendRequest(*request);
444
445 Poco::MongoDB::Document::Ptr lastError = db.getLastErrorDoc(connection);
446 std::cout << "LastError: " << lastError->toString(2) << std::endl;
447}
448
449
450int main(int argc, char** argv)
451{
452 Poco::MongoDB::Connection connection("localhost", 27017);
453
454 try
455 {
456 sample1(connection);
457 sample2(connection);
458 sample3(connection);
459 sample4(connection);
460 sample5(connection);
461 sample6(connection);
462 sample7(connection);
463 sample8(connection);
464 sample9(connection);
465 sample10(connection);
466 sample11(connection);
467 sample12(connection);
468 sample13(connection);
469 }
470 catch (Poco::Exception& exc)
471 {
472 std::cerr << exc.displayText() << std::endl;
473 }
474
475 return 0;
476}
477