mirror of
https://github.com/pocoproject/poco.git
synced 2025-01-19 00:46:03 +01:00
Merge pull request #115 from fbraem/develop
MongoDB: more samples and some minor changes
This commit is contained in:
commit
3c7bae11fc
@ -42,6 +42,8 @@
|
||||
#include "Poco/MongoDB/Document.h"
|
||||
#include "Poco/MongoDB/QueryRequest.h"
|
||||
#include "Poco/MongoDB/InsertRequest.h"
|
||||
#include "Poco/MongoDB/UpdateRequest.h"
|
||||
#include "Poco/MongoDB/DeleteRequest.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
@ -75,6 +77,11 @@ public:
|
||||
/// the database name!
|
||||
|
||||
|
||||
Poco::SharedPtr<Poco::MongoDB::DeleteRequest> createDeleteRequest(const std::string& collectionName) const;
|
||||
/// Creates a DeleteRequest to delete documents in the given collection.
|
||||
/// The collectionname must not contain the database name!
|
||||
|
||||
|
||||
Poco::SharedPtr<Poco::MongoDB::InsertRequest> createInsertRequest(const std::string& collectionName) const;
|
||||
/// Creates an InsertRequest to insert new documents in the given collection.
|
||||
/// The collectionname must not contain the database name!
|
||||
@ -84,6 +91,10 @@ public:
|
||||
/// Creates a QueryRequest. The collectionname must not contain the database name!
|
||||
|
||||
|
||||
Poco::SharedPtr<Poco::MongoDB::UpdateRequest> createUpdateRequest(const std::string& collectionName) const;
|
||||
/// Creates an UpdateRequest. The collectionname must not contain the database name!
|
||||
|
||||
|
||||
Poco::MongoDB::Document::Ptr ensureIndex(Connection& connection, const std::string& collection, const std::string& indexName, Poco::MongoDB::Document::Ptr keys, bool unique = false, bool background = false, int version = 0, int ttl = 0);
|
||||
/// Creates an index. The document returned is the result of a getLastError call.
|
||||
/// For more info look at the ensureIndex information on the MongoDB website.
|
||||
@ -109,6 +120,30 @@ inline Poco::SharedPtr<Poco::MongoDB::QueryRequest> Database::createCommand() co
|
||||
return cmd;
|
||||
}
|
||||
|
||||
|
||||
inline Poco::SharedPtr<Poco::MongoDB::DeleteRequest> Database::createDeleteRequest(const std::string& collectionName) const
|
||||
{
|
||||
return new Poco::MongoDB::DeleteRequest(_dbname + '.' + collectionName);
|
||||
}
|
||||
|
||||
|
||||
inline Poco::SharedPtr<Poco::MongoDB::InsertRequest> Database::createInsertRequest(const std::string& collectionName) const
|
||||
{
|
||||
return new Poco::MongoDB::InsertRequest(_dbname + '.' + collectionName);
|
||||
}
|
||||
|
||||
|
||||
inline Poco::SharedPtr<Poco::MongoDB::QueryRequest> Database::createQueryRequest(const std::string& collectionName) const
|
||||
{
|
||||
return new Poco::MongoDB::QueryRequest(_dbname + '.' + collectionName);
|
||||
}
|
||||
|
||||
|
||||
inline Poco::SharedPtr<Poco::MongoDB::UpdateRequest> Database::createUpdateRequest(const std::string& collectionName) const
|
||||
{
|
||||
return new Poco::MongoDB::UpdateRequest(_dbname + '.' + collectionName);
|
||||
}
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif // _MongoDB_Database_included
|
||||
|
@ -368,7 +368,7 @@ void sample9(Poco::MongoDB::Connection& connection)
|
||||
connection.sendRequest(query, response);
|
||||
if ( response.hasDocuments() )
|
||||
{
|
||||
std::cout << response.documents()[0]->toString(2);
|
||||
std::cout << response.documents()[0]->toString(2) << std::endl;
|
||||
}
|
||||
|
||||
// QueryRequest can be created using the Database class
|
||||
@ -378,7 +378,7 @@ void sample9(Poco::MongoDB::Connection& connection)
|
||||
connection.sendRequest(*queryPtr, response);
|
||||
if ( response.hasDocuments() )
|
||||
{
|
||||
std::cout << response.documents()[0]->toString(2);
|
||||
std::cout << response.documents()[0]->toString(2) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -389,7 +389,6 @@ void sample10(Poco::MongoDB::Connection& connection)
|
||||
|
||||
Poco::MongoDB::Database db("sample");
|
||||
Poco::SharedPtr<Poco::MongoDB::QueryRequest> command = db.createCommand();
|
||||
std::cout << command->fullCollectionName() << std::endl;
|
||||
|
||||
command->selector()
|
||||
.add("distinct", "players")
|
||||
@ -411,6 +410,61 @@ void sample10(Poco::MongoDB::Connection& connection)
|
||||
|
||||
}
|
||||
|
||||
// SELECT COUNT(*) FROM players WHERE birthyear > 1980
|
||||
void sample11(Poco::MongoDB::Connection& connection)
|
||||
{
|
||||
std::cout << "*** SAMPLE 11 ***" << std::endl;
|
||||
|
||||
Poco::MongoDB::Database db("sample");
|
||||
Poco::SharedPtr<Poco::MongoDB::QueryRequest> count = db.createCountRequest("players");
|
||||
count->selector().addNewDocument("query")
|
||||
.addNewDocument("birthyear")
|
||||
.add("$gt", 1980);
|
||||
|
||||
Poco::MongoDB::ResponseMessage response;
|
||||
connection.sendRequest(*count, response);
|
||||
|
||||
if ( response.hasDocuments() )
|
||||
{
|
||||
std::cout << "Count: " << response.documents()[0]->get<double>("n") << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//UPDATE players SET birthyear = birthyear + 1 WHERE firstname = 'Victor'
|
||||
void sample12(Poco::MongoDB::Connection& connection)
|
||||
{
|
||||
std::cout << "*** SAMPLE 12 ***" << std::endl;
|
||||
|
||||
Poco::MongoDB::Database db("sample");
|
||||
Poco::SharedPtr<Poco::MongoDB::UpdateRequest> request = db.createUpdateRequest("players");
|
||||
request->selector().add("firstname", "Victor");
|
||||
|
||||
request->update().addNewDocument("$inc").add("birthyear", 1);
|
||||
|
||||
connection.sendRequest(*request);
|
||||
|
||||
Poco::MongoDB::Document::Ptr lastError = db.getLastErrorDoc(connection);
|
||||
std::cout << "LastError: " << lastError->toString(2) << std::endl;
|
||||
}
|
||||
|
||||
|
||||
//DELETE players WHERE firstname = 'Victor'
|
||||
void sample13(Poco::MongoDB::Connection& connection)
|
||||
{
|
||||
std::cout << "*** SAMPLE 13 ***" << std::endl;
|
||||
|
||||
Poco::MongoDB::Database db("sample");
|
||||
Poco::SharedPtr<Poco::MongoDB::DeleteRequest> request = db.createDeleteRequest("players");
|
||||
request->selector().add("firstname", "Victor");
|
||||
|
||||
connection.sendRequest(*request);
|
||||
|
||||
Poco::MongoDB::Document::Ptr lastError = db.getLastErrorDoc(connection);
|
||||
std::cout << "LastError: " << lastError->toString(2) << std::endl;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Poco::MongoDB::Connection connection("localhost", 27017);
|
||||
@ -425,6 +479,9 @@ int main(int argc, char** argv)
|
||||
sample8(connection);
|
||||
sample9(connection);
|
||||
sample10(connection);
|
||||
sample11(connection);
|
||||
sample12(connection);
|
||||
sample13(connection);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -147,16 +147,4 @@ Poco::SharedPtr<Poco::MongoDB::QueryRequest> Database::createCountRequest(const
|
||||
}
|
||||
|
||||
|
||||
Poco::SharedPtr<Poco::MongoDB::InsertRequest> Database::createInsertRequest(const std::string& collectionName) const
|
||||
{
|
||||
return new Poco::MongoDB::InsertRequest(_dbname + '.' + collectionName);
|
||||
}
|
||||
|
||||
|
||||
Poco::SharedPtr<Poco::MongoDB::QueryRequest> Database::createQueryRequest(const std::string& collectionName) const
|
||||
{
|
||||
return new Poco::MongoDB::QueryRequest(_dbname + '.' + collectionName);
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
@ -150,7 +150,13 @@ void Document::read(BinaryReader& reader)
|
||||
std::string Document::toString(int indent) const
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "{" << std::endl;
|
||||
|
||||
oss << "{";
|
||||
if ( indent > 0 )
|
||||
{
|
||||
oss << std::endl;
|
||||
}
|
||||
|
||||
for(ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it)
|
||||
{
|
||||
if ( it != _elements.begin() )
|
||||
@ -189,7 +195,8 @@ std::string Document::toString(int indent) const
|
||||
}
|
||||
}
|
||||
|
||||
oss << "}" << std::endl;
|
||||
oss << "}";
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user