Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/mongo/client/dbclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,24 @@ namespace mongo {
return ok;
}

bool DBClientWithCommands::createCollection(const string &ns, long long size, bool capped, int max, BSONObj *info) {
bool DBClientWithCommands::createCollection(
const string &ns,
long long size,
bool capped,
int max,
BSONObj *info
) {
return createCollectionWithOptions(ns, size, capped, max, BSONObj(), info);
}

bool DBClientWithCommands::createCollectionWithOptions(
const string &ns,
long long size,
bool capped,
int max,
const BSONObj& extra,
BSONObj *info
) {
verify(!capped||size);
BSONObj o;
if ( info == 0 ) info = &o;
Expand All @@ -733,6 +750,7 @@ namespace mongo {
if ( size ) b.append("size", size);
if ( capped ) b.append("capped", true);
if ( max ) b.append("max", max);
if ( !extra.isEmpty() ) b.appendElements(extra);
return runCommand(db.c_str(), b.done(), *info);
}

Expand Down
35 changes: 33 additions & 2 deletions src/mongo/client/dbclientinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -682,9 +682,40 @@ namespace mongo {
@param capped if true, this is a fixed size collection (where old data rolls out).
@param max maximum number of objects if capped (optional).

returns true if successful.
@return true if successful.
*/
bool createCollection(const std::string &ns, long long size = 0, bool capped = false, int max = 0, BSONObj *info = 0);
bool createCollection(
const std::string &ns,
long long size = 0,
bool capped = false,
int max = 0,
BSONObj *info = 0
);

/**
* Creates a new collection in the database. Allows for a user to provide a BSONObj that
* contains extra options.
*
* @param extraOptions Add extra parameters to the create collection command for features
* that are version specific or for which default values have flipped between server
* releases. Some examples are "usePowerOf2Sizes" and "autoIndexId".
*
* @warning Options set in extraOptions which shadow those passed in as parameters will
* have indeterminate behavior.
*
* @see the form of createCollection with less parameters above.
*
* @see http://docs.mongodb.org/manual/reference/command/create/#dbcmd.create for available
* options.
*/
bool createCollectionWithOptions(
const std::string &ns,
long long size = 0,
bool capped = false,
int max = 0,
const BSONObj& extraOptions = BSONObj(),
BSONObj *info = 0
);

/** Get error result from the last write operation (insert/update/delete) on this connection.
db doesn't change the command's behavior - it is just for auth checks.
Expand Down
21 changes: 21 additions & 0 deletions src/mongo/unittest/dbclient_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,27 @@ namespace {
ASSERT_TRUE(c.createCollection(TEST_NS));
ASSERT_FALSE(c.createCollection(TEST_NS));
ASSERT_TRUE(c.exists(TEST_NS));

BSONObj info;
ASSERT_TRUE(c.runCommand(TEST_DB, BSON("collstats" << TEST_COLL), info));

bool server26plus = serverGTE(&c, 2, 6);

ASSERT_EQUALS(info.getIntField("userFlags"), server26plus ? 1 : 0);
ASSERT_EQUALS(info.getIntField("nindexes"), 1);
}

TEST_F(DBClientTest, CreateCollectionAdvanced) {
BSONObjBuilder opts;
opts.append("usePowerOf2Sizes", 0);
opts.append("autoIndexId", false);
c.createCollectionWithOptions(TEST_NS, 1, true, 1, opts.obj());

BSONObj info;
ASSERT_TRUE(c.runCommand(TEST_DB, BSON("collstats" << TEST_COLL), info));

ASSERT_EQUALS(info.getIntField("userFlags"), 0);
ASSERT_EQUALS(info.getIntField("nindexes"), 0);
}

TEST_F(DBClientTest, CopyDatabase) {
Expand Down