public Mono<String> ensureIndex(final IndexDefinition indexDefinition) { return mongoOperations.execute(collectionName, collection -> { Document indexOptions = indexDefinition.getIndexOptions(); IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition); if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) { Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY)); MongoPersistentEntity<?> entity = type .map(val -> (MongoPersistentEntity) queryMapper.getMappingContext().getRequiredPersistentEntity(val)) .orElseGet(() -> lookupPersistentEntity(collectionName)); ops = ops.partialFilterExpression( queryMapper.getMappedObject(indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY, Document.class), entity)); } return collection.createIndex(indexDefinition.getIndexKeys(), ops); }).next(); }
public String ensureIndex(final IndexDefinition indexDefinition) { return execute(collection -> { Document indexOptions = indexDefinition.getIndexOptions(); IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition); if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) { Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY)); ops.partialFilterExpression(mapper.getMappedObject((Document) indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY), lookupPersistentEntity(type, collectionName))); } return collection.createIndex(indexDefinition.getIndexKeys(), ops); }); }
@SuppressWarnings("deprecation") com.mongodb.client.model.IndexOptions convert(final IndexOptions options, final boolean background) { if (options.dropDups()) { LOG.warn("Support for dropDups has been removed from the server. Please remove this setting."); } com.mongodb.client.model.IndexOptions indexOptions = new com.mongodb.client.model.IndexOptions() .background(options.background() || background) .sparse(options.sparse()) .unique(options.unique()); if (!options.language().equals("")) { indexOptions.defaultLanguage(options.language()); } if (!options.languageOverride().equals("")) { indexOptions.languageOverride(options.languageOverride()); } if (!options.name().equals("")) { indexOptions.name(options.name()); } if (options.expireAfterSeconds() != -1) { indexOptions.expireAfter((long) options.expireAfterSeconds(), TimeUnit.SECONDS); } if (!options.partialFilter().equals("")) { indexOptions.partialFilterExpression(Document.parse(options.partialFilter())); } if (!options.collation().locale().equals("")) { indexOptions.collation(convert(options.collation())); } return indexOptions; }
ops = ops.partialFilterExpression((org.bson.Document) indexOptions.get("partialFilterExpression"));
public Mono<String> ensureIndex(final IndexDefinition indexDefinition) { return mongoOperations.execute(collectionName, collection -> { Document indexOptions = indexDefinition.getIndexOptions(); IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition); if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) { Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY)); MongoPersistentEntity<?> entity = type .map(val -> (MongoPersistentEntity) queryMapper.getMappingContext().getRequiredPersistentEntity(val)) .orElseGet(() -> lookupPersistentEntity(collectionName)); ops = ops.partialFilterExpression( queryMapper.getMappedObject(indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY, Document.class), entity)); } return collection.createIndex(indexDefinition.getIndexKeys(), ops); }).next(); }
public String ensureIndex(final IndexDefinition indexDefinition) { return execute(collection -> { Document indexOptions = indexDefinition.getIndexOptions(); IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition); if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) { Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY)); ops.partialFilterExpression(mapper.getMappedObject((Document) indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY), lookupPersistentEntity(type, collectionName))); } return collection.createIndex(indexDefinition.getIndexKeys(), ops); }); }
ops = ops.partialFilterExpression((org.bson.Document) indexOptions.get("partialFilterExpression"));
/** * Forces creation of a partial index on a set of fields, if one does not * already exist. * * @param collection the collection. * @param fields the name of the fields. * @param ascending {@code true} for an ascending, {@code false} for a * descending index. * @param filter the filter expression for the partial index. * @throws MongoException if the operation fails. */ 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); }
/** * Forces creation of a partial index on a set of fields, if one does not * already exist. * * @param collection the collection. * @param fields the name of the fields. * @param ascending {@code true} for an ascending, {@code false} for a * descending index. * @param filter the filter expression for the partial index. * @throws MongoException if the operation fails. */ 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); }
@Test public void testAddPartialIndexOnNonIdField() { collection.insertOne(json("someField: 'abc'")); assertThat(toArray(collection.listIndexes())).hasSize(1); collection.createIndex(new Document("someField", 1), new IndexOptions() .partialFilterExpression(json("someField: {$gt: 5}"))); assertThat(toArray(collection.listIndexes())).hasSize(2); collection.insertOne(json("someField: 'abc'")); }
break; case "partialFilterExpression": indexOptions.partialFilterExpression((Bson) value); break; case "collation":
/** * Creates a new {@link IndexModel}, which can be used for creating indices using MongoDB Java drivers. * * @return the created {@link IndexModel} */ public IndexModel toIndexModel() { final IndexOptions options = new IndexOptions() .name(name) .unique(unique) .sparse(sparse) .background(background); if (!partialFilterExpression.isEmpty()) { options.partialFilterExpression(partialFilterExpression); } return new IndexModel(keys, options); }
/** * Creates a new {@link IndexModel}, which can be used for creating indices using MongoDB Java drivers. * * @return the created {@link IndexModel} */ public IndexModel toIndexModel() { final IndexOptions options = new IndexOptions() .name(name) .unique(unique) .sparse(sparse) .background(background); if (!partialFilterExpression.isEmpty()) { options.partialFilterExpression(partialFilterExpression); } return new IndexModel(keys, options); }
@SuppressWarnings("deprecation") com.mongodb.client.model.IndexOptions convert(final IndexOptions options, final boolean background) { if (options.dropDups()) { LOG.warning("Support for dropDups has been removed from the server. Please remove this setting."); } com.mongodb.client.model.IndexOptions indexOptions = new com.mongodb.client.model.IndexOptions() .background(options.background() || background) .sparse(options.sparse()) .unique(options.unique()); if (!options.language().equals("")) { indexOptions.defaultLanguage(options.language()); } if (!options.languageOverride().equals("")) { indexOptions.languageOverride(options.languageOverride()); } if (!options.name().equals("")) { indexOptions.name(options.name()); } if (options.expireAfterSeconds() != -1) { indexOptions.expireAfter((long) options.expireAfterSeconds(), TimeUnit.SECONDS); } if (!options.partialFilter().equals("")) { indexOptions.partialFilterExpression(Document.parse(options.partialFilter())); } if (!options.collation().locale().equals("")) { indexOptions.collation(convert(options.collation())); } return indexOptions; }
indexOptions.partialFilterExpression( (Bson) options.get( "partialFilterExpression" ) );
indexOptions.partialFilterExpression( (Bson) options.get( "partialFilterExpression" ) );
mongoDbIndexOptions.partialFilterExpression(indexOptions.get("partialFilterExpression", Bson.class));
applyIfTrue(options.containsKey("name"), () -> indexOptions.name(options.getString("name"))); applyIfTrue(options.containsKey("partialFilterExpression"), () -> indexOptions.partialFilterExpression(options.get("partialFilterExpression", Bson.class))); applyIfTrue(options.containsKey("sparse"), () -> indexOptions.sparse(options.getBoolean("sparse"))); applyIfTrue(options.containsKey("sphereVersion"), () -> indexOptions.sphereVersion(options.getInteger("sphereVersion")));
ret.partialFilterExpression(options.get("partialFilterExpression") .asDocument());