Skip to content

Commit 342b0bb

Browse files
committed
CXX-685: Support Filtered (Partial) Indexes
1 parent 76be500 commit 342b0bb

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

src/mongo/client/index_spec.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ IndexSpec& IndexSpec::version(int value) {
124124
return *this;
125125
}
126126

127+
IndexSpec& IndexSpec::partialFilterExpression(const BSONObj& value) {
128+
uassert(0, kDuplicateOption, !_options.asTempObj().hasField("partialFilterExpression"));
129+
_options.append("partialFilterExpression", value);
130+
return *this;
131+
}
132+
127133
IndexSpec& IndexSpec::textWeights(const BSONObj& value) {
128134
uassert(0, kDuplicateOption, !_options.asTempObj().hasField("weights"));
129135
_options.append("weights", value);

src/mongo/client/index_spec.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ class MONGO_CLIENT_API IndexSpec {
125125
*/
126126
IndexSpec& version(int value);
127127

128+
/** Sets the 'partialFilterExpression' document for a partial index. */
129+
IndexSpec& partialFilterExpression(const BSONObj& value);
128130

129131
//
130132
// Text options

src/mongo/client/index_spec_test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ TEST(Options, RepeatedOptionsFail) {
3030
ASSERT_UASSERTS(IndexSpec().sparse().sparse());
3131
ASSERT_UASSERTS(IndexSpec().expireAfterSeconds(1).expireAfterSeconds(1));
3232
ASSERT_UASSERTS(IndexSpec().version(0).version(0));
33+
ASSERT_UASSERTS(
34+
IndexSpec().partialFilterExpression(BSONObj()).partialFilterExpression(BSONObj()));
3335
ASSERT_UASSERTS(IndexSpec().textWeights(BSONObj()).textWeights(BSONObj()));
3436
ASSERT_UASSERTS(IndexSpec().textDefaultLanguage("foo").textDefaultLanguage("foo"));
3537
ASSERT_UASSERTS(IndexSpec().textLanguageOverride("foo").textLanguageOverride("foo"));

src/mongo/integration/standalone/dbclient_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,19 @@ TEST_F(DBClientTest, CreateHashedIndex) {
14091409
c->createIndex(TEST_NS, IndexSpec().addKey("aField", IndexSpec::kIndexTypeHashed));
14101410
}
14111411

1412+
TEST_F(DBClientTest, CreatePartialIndex) {
1413+
if (serverGTE(c.get(), 3, 1)) {
1414+
c->createIndex(
1415+
TEST_NS,
1416+
IndexSpec().addKey("aField").partialFilterExpression(BSON("aField" << GT << 5)));
1417+
1418+
std::list<BSONObj> indexes = c->getIndexSpecs(TEST_NS);
1419+
ASSERT_EQUALS(2U, indexes.size());
1420+
indexes.pop_front();
1421+
ASSERT_EQUALS(BSON("aField" << GT << 5), indexes.front()["partialFilterExpression"].Obj());
1422+
}
1423+
}
1424+
14121425
TEST_F(DBClientTest, CreateUser) {
14131426
createUser(c.get(), TEST_DB, "user1", "password1");
14141427
}

0 commit comments

Comments
 (0)