mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-01 09:24:55 +02:00
Add ensureIndex
This commit is contained in:
parent
7328eace34
commit
60afc2dbd0
@ -80,6 +80,11 @@ public:
|
||||
/// 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.
|
||||
|
||||
|
||||
Document::Ptr getLastErrorDoc(Connection& connection) const;
|
||||
/// Sends the getLastError command to the database and returns the document
|
||||
|
||||
|
@ -69,6 +69,44 @@ double Database::count(Connection& connection, const std::string& collectionName
|
||||
}
|
||||
|
||||
|
||||
Poco::MongoDB::Document::Ptr Database::ensureIndex(Connection& connection, const std::string& collection, const std::string& indexName, Poco::MongoDB::Document::Ptr keys, bool unique, bool background, int version, int ttl)
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr index = new Poco::MongoDB::Document();
|
||||
index->add("ns", _dbname + ".players");
|
||||
index->add("name", indexName);
|
||||
index->add("key", keys);
|
||||
|
||||
if ( version > 0 )
|
||||
{
|
||||
index->add("version", version);
|
||||
}
|
||||
|
||||
if ( unique )
|
||||
{
|
||||
index->add("unique", true);
|
||||
}
|
||||
|
||||
if ( background )
|
||||
{
|
||||
index->add("background", true);
|
||||
}
|
||||
|
||||
if ( ttl > 0 )
|
||||
{
|
||||
index->add("expireAfterSeconds", ttl);
|
||||
}
|
||||
|
||||
Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest = createInsertRequest("system.indexes");
|
||||
insertRequest->documents().push_back(index);
|
||||
connection.sendRequest(*insertRequest);
|
||||
|
||||
insertRequest->documents().push_back(index);
|
||||
connection.sendRequest(*insertRequest);
|
||||
|
||||
return getLastErrorDoc(connection);
|
||||
}
|
||||
|
||||
|
||||
Document::Ptr Database::getLastErrorDoc(Connection& connection) const
|
||||
{
|
||||
Document::Ptr errorDoc;
|
||||
|
Loading…
x
Reference in New Issue
Block a user