@Test public void createPartialIndex() { MongoConnection c = connectionFactory.getConnection(); assertNotNull(c); c.getDatabase().drop(); MongoStatus status = new MongoStatus(c.getMongoClient(), c.getDBName()); assumeTrue(status.isVersion(3, 2)); MongoCollection collection = c.getDatabase().getCollection("test"); MongoUtils.createPartialIndex(collection, new String[]{"foo", "bar"}, new boolean[]{true, true}, "{foo:true}"); assertTrue(MongoUtils.hasIndex(collection, "_id")); assertTrue(MongoUtils.hasIndex(collection, "foo", "bar")); List<Document> indexes = new ArrayList<>(); collection.listIndexes().into(indexes); assertEquals(2, indexes.size()); for (Document info : indexes) { Document key = (Document) info.get("key"); assertNotNull(key); if (key.keySet().contains("foo")) { assertEquals(2, key.keySet().size()); assertEquals(1, key.get("foo")); assertEquals(1, key.get("bar")); Document filter = (Document) info.get("partialFilterExpression"); assertNotNull(filter); assertEquals(Boolean.TRUE, filter.get("foo")); } } c.getDatabase().drop(); }
static void createPartialIndex(MongoCollection<?> collection, String[] fields, boolean[] ascending, String filter) throws MongoException { checkArgument(fields.length == ascending.length); BasicDBObject index = new BasicDBObject(); for (int i = 0; i < fields.length; i++) { index.put(fields[i], ascending[i] ? 1 : -1); } IndexOptions options = new IndexOptions().partialFilterExpression(BasicDBObject.parse(filter)); collection.createIndex(index, options); }
MongoUtils { static void createPartialIndex(MongoCollection<?> collection, String[] fields, boolean[] ascending, String filter) throws MongoException { checkArgument(fields.length == ascending.length); BasicDBObject index = new BasicDBObject(); for (int i = 0; i < fields.length; i++) { index.put(fields[i], ascending[i] ? 1 : -1); } IndexOptions options = new IndexOptions().partialFilterExpression(BasicDBObject.parse(filter)); collection.createIndex(index, options); } }
MongoUtils { static void createPartialIndex(MongoCollection<?> collection, String[] fields, boolean[] ascending, String filter) throws MongoException { checkArgument(fields.length == ascending.length); BasicDBObject index = new BasicDBObject(); for (int i = 0; i < fields.length; i++) { index.put(fields[i], ascending[i] ? 1 : -1); } IndexOptions options = new IndexOptions().partialFilterExpression(BasicDBObject.parse(filter)); collection.createIndex(index, options); }  }
MongoUtils { static void createPartialIndex(MongoCollection<?> collection, String[] fields, boolean[] ascending, String filter) throws MongoException { checkArgument(fields.length == ascending.length); BasicDBObject index = new BasicDBObject(); for (int i = 0; i < fields.length; i++) { index.put(fields[i], ascending[i] ? 1 : -1); } IndexOptions options = new IndexOptions().partialFilterExpression(BasicDBObject.parse(filter)); collection.createIndex(index, options); }   }
MongoUtils { static void createPartialIndex(MongoCollection<?> collection, String[] fields, boolean[] ascending, String filter) throws MongoException { checkArgument(fields.length == ascending.length); BasicDBObject index = new BasicDBObject(); for (int i = 0; i < fields.length; i++) { index.put(fields[i], ascending[i] ? 1 : -1); } IndexOptions options = new IndexOptions().partialFilterExpression(BasicDBObject.parse(filter)); collection.createIndex(index, options); }    }