@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.sparse((Boolean) indexOptions.get("sparse"));
@Override public IndexOptions convert(Environment env, TraceInfo trace, Memory arg) throws Throwable { if (arg.isNull()) return null; ArrayMemory arr = arg.toValue(ArrayMemory.class); IndexOptions options = new IndexOptions(); if (arr.containsKey("background")) { options.background(arg.valueOfIndex("background").toBoolean()); } if (arr.containsKey("defaultLanguage")) { options.defaultLanguage(arg.valueOfIndex("defaultLanguage").toString()); } if (arr.containsKey("bits")) { options.bits(arg.valueOfIndex("bits").toInteger()); } if (arr.containsKey("name")) { options.name(arg.valueOfIndex("name").toString()); } if (arr.containsKey("max")) { options.max(arg.valueOfIndex("max").toDouble()); } if (arr.containsKey("min")) { options.min(arg.valueOfIndex("min").toDouble()); } if (arr.containsKey("languageOverride")) { options.languageOverride(arg.valueOfIndex("languageOverride").toString()); } if (arr.containsKey("sparse")) { options.sparse(arg.valueOfIndex("sparse").toBoolean()); } if (arr.containsKey("unique")) { options.unique(arg.valueOfIndex("unique").toBoolean()); } if (arr.containsKey("version")) { options.version(arg.valueOfIndex("version").toInteger()); } if (arr.containsKey("textVersion")) { options.textVersion(arg.valueOfIndex("textVersion").toInteger()); } if (arr.containsKey("sphereVersion")) { options.sphereVersion(arg.valueOfIndex("sphereVersion").toInteger()); } return options; }
ops = ops.sparse((Boolean) indexOptions.get("sparse"));
/** * Forces creation of an 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 unique whether values are unique. * @param sparse whether the index should be sparse. * @throws IllegalArgumentException if {@code fields} and {@code ascending} * arrays have different lengths. * @throws MongoException if the operation fails. */ static void createIndex(MongoCollection<?> collection, String[] fields, boolean[] ascending, boolean unique, boolean sparse) 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().unique(unique).sparse(sparse); collection.createIndex(index, options); }
@Test public void testAddSparseIndexOnNonIdField() { collection.insertOne(json("someField: 'abc'")); assertThat(toArray(collection.listIndexes())).hasSize(1); collection.createIndex(new Document("someField", 1), new IndexOptions().sparse(true)); assertThat(toArray(collection.listIndexes())).hasSize(2); collection.insertOne(json("someField: 'abc'")); }
/** * Forces creation of an 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 unique whether values are unique. * @param sparse whether the index should be sparse. * @throws IllegalArgumentException if {@code fields} and {@code ascending} * arrays have different lengths. * @throws MongoException if the operation fails. */ static void createIndex(MongoCollection<?> collection, String[] fields, boolean[] ascending, boolean unique, boolean sparse) 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().unique(unique).sparse(sparse); collection.createIndex(index, 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); }
/** * 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); }
/** * Ensure that expected collection indexes exist. * * @throws SiteWhereException */ protected void ensureIndexes() throws SiteWhereException { getMongoClient().getEventsCollection(getTenant()).createIndex( new BasicDBObject(MongoDeviceEvent.PROP_ALTERNATE_ID, 1), new IndexOptions().unique(true).sparse(true)); getMongoClient().getEventsCollection(getTenant()) .createIndex(new BasicDBObject(MongoDeviceEvent.PROP_DEVICE_ASSIGNMENT_TOKEN, 1) .append(MongoDeviceEvent.PROP_EVENT_DATE, -1).append(MongoDeviceEvent.PROP_EVENT_TYPE, 1)); getMongoClient().getEventsCollection(getTenant()) .createIndex(new BasicDBObject(MongoDeviceEvent.PROP_SITE_TOKEN, 1) .append(MongoDeviceEvent.PROP_EVENT_DATE, -1).append(MongoDeviceEvent.PROP_EVENT_TYPE, 1)); }
/** * Ensure that expected collection indexes exist. * * @throws SiteWhereException */ protected void ensureIndexes() throws SiteWhereException { getMongoClient().getEventsCollection().createIndex(new BasicDBObject(MongoDeviceEvent.PROP_ALTERNATE_ID, 1), new IndexOptions().unique(true).sparse(true)); getMongoClient().getEventsCollection() .createIndex(new BasicDBObject(MongoDeviceEvent.PROP_DEVICE_ASSIGNMENT_ID, 1) .append(MongoDeviceEvent.PROP_EVENT_TYPE, 1).append(MongoDeviceEvent.PROP_EVENT_DATE, -1)); getMongoClient().getEventsCollection().createIndex(new BasicDBObject(MongoDeviceEvent.PROP_CUSTOMER_ID, 1) .append(MongoDeviceEvent.PROP_EVENT_TYPE, 1).append(MongoDeviceEvent.PROP_EVENT_DATE, -1)); getMongoClient().getEventsCollection().createIndex(new BasicDBObject(MongoDeviceEvent.PROP_AREA_ID, 1) .append(MongoDeviceEvent.PROP_EVENT_TYPE, 1).append(MongoDeviceEvent.PROP_EVENT_DATE, -1)); getMongoClient().getEventsCollection().createIndex(new BasicDBObject(MongoDeviceEvent.PROP_ASSET_ID, 1) .append(MongoDeviceEvent.PROP_EVENT_TYPE, 1).append(MongoDeviceEvent.PROP_EVENT_DATE, -1)); }
public QueryResult createIndex(Bson keys, ObjectMap options) { long start = startQuery(); IndexOptions i = new IndexOptions(); if (options.containsKey(UNIQUE)) { i.unique(options.getBoolean(UNIQUE)); } if (options.containsKey(BACKGROUND)) { i.background(options.getBoolean(BACKGROUND)); } if (options.containsKey(SPARSE)) { i.sparse(options.getBoolean(SPARSE)); } if (options.containsKey(NAME)) { i.name(options.getString(NAME)); } mongoDBNativeQuery.createIndex(keys, i); QueryResult queryResult = endQuery(Collections.emptyList(), start); return queryResult; }
@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; }
@Test public void testSecondarySparseUniqueIndex() throws Exception { collection.createIndex(json("text: 1"), new IndexOptions().unique(true).sparse(true)); collection.insertOne(json("_id: 1, text: 'abc'")); collection.insertOne(json("_id: 2, text: 'def'")); collection.insertOne(json("_id: 3")); collection.insertOne(json("_id: 4")); collection.insertOne(json("_id: 5, text: null")); assertMongoWriteException(() -> collection.insertOne(json("_id: 6, text: null")), 11000, "E11000 duplicate key error collection: testdb.testcoll index: text_1 dup key: { : null }"); assertMongoWriteException(() -> collection.insertOne(json("_id: 7, text: 'abc'")), 11000, "E11000 duplicate key error collection: testdb.testcoll index: text_1 dup key: { : \"abc\" }"); assertMongoWriteException(() -> collection.updateOne(json("_id: 2"), new Document("$set", json("text: null"))), 11000, "E11000 duplicate key error collection: testdb.testcoll index: text_1 dup key: { : null }"); collection.deleteOne(json("_id: 5")); collection.updateOne(json("_id: 2"), new Document("$set", json("text: null"))); collection.updateOne(json("_id: 1"), new Document("$set", json("text: 'def'"))); collection.deleteMany(json("text: null")); assertThat(toArray(collection.find())).containsExactly(json("_id: 1, text: 'def'")); }
indexOptions.sparse( !options.containsKey( "partialFilterExpression" ) );
indexOptions.sparse( !options.containsKey( "partialFilterExpression" ) );
@Test public void testCompoundSparseUniqueIndex() throws Exception { collection.createIndex(json("a: 1, b: 1"), new IndexOptions().unique(true).sparse(true)); collection.insertOne(json("_id: 1, a: 10, b: 20")); collection.insertOne(json("_id: 2, a: 10")); collection.insertOne(json("_id: 3, b: 20")); collection.insertOne(json("_id: 4")); collection.insertOne(json("_id: 5")); collection.insertOne(json("_id: 6, a: null")); assertMongoWriteException(() -> collection.insertOne(json("_id: 7, a: null")), 11000, "E11000 duplicate key error collection: testdb.testcoll index: a_1_b_1 dup key: { : null, : null }"); assertMongoWriteException(() -> collection.insertOne(json("_id: 7, b: null")), 11000, "E11000 duplicate key error collection: testdb.testcoll index: a_1_b_1 dup key: { : null, : null }"); collection.deleteMany(json("a: null, b: null")); assertThat(toArray(collection.find())) .containsExactlyInAnyOrder( json("_id: 1, a: 10, b: 20"), json("_id: 2, a: 10"), json("_id: 3, b: 20") ); }
@Test public void testCompoundSparseUniqueIndexOnEmbeddedDocuments() throws Exception { collection.createIndex(json("'a.x': 1, 'b.x': 1"), new IndexOptions().unique(true).sparse(true)); collection.insertOne(json("_id: 1, a: 10, b: 20")); collection.insertOne(json("_id: 2, a: 10")); collection.insertOne(json("_id: 3, b: 20")); collection.insertOne(json("_id: 4, a: {x: 1}")); collection.insertOne(json("_id: 5, b: {x: 2}")); collection.insertOne(json("_id: 6, a: {x: 1}, b: {x: 2}")); collection.insertOne(json("_id: 7, a: {x: 2}, b: {x: 2}")); collection.insertOne(json("_id: 8, a: {x: null}, b: {x: null}")); collection.insertOne(json("_id: 9")); assertMongoWriteException(() -> collection.insertOne(json("_id: 10, a: {x: 1.0}, b: {x: 2.0}")), 11000, "E11000 duplicate key error collection: testdb.testcoll index: a.x_1_b.x_1 dup key: { : 1.0, : 2.0 }"); assertMongoWriteException(() -> collection.insertOne(json("_id: 11, a: {x: null}, b: {x: null}")), 11000, "E11000 duplicate key error collection: testdb.testcoll index: a.x_1_b.x_1 dup key: { : null, : null }"); collection.deleteMany(json("a: {x: null}, b: {x: null}")); collection.deleteMany(json("a: 10")); collection.deleteMany(json("b: 20")); assertThat(toArray(collection.find())) .containsExactlyInAnyOrder( json("_id: 4, a: {x: 1}"), json("_id: 5, b: {x: 2}"), json("_id: 6, a: {x: 1}, b: {x: 2}"), json("_id: 7, a: {x: 2}, b: {x: 2}"), json("_id: 9") ); }
@Test public void testSparseUniqueIndexOnEmbeddedDocument() throws Exception { collection.createIndex(json("'a.b.c': 1"), new IndexOptions().unique(true).sparse(true)); collection.insertOne(json("a: 1")); collection.insertOne(json("a: 1")); collection.insertOne(json("a: null")); collection.insertOne(json("a: null")); collection.insertOne(json("a: {b: 1}")); collection.insertOne(json("a: {b: 1}")); collection.insertOne(json("a: {b: null}")); collection.insertOne(json("a: {b: null}")); collection.insertOne(json("a: {b: {c: 1}}")); collection.insertOne(json("a: {b: {c: 2}}")); collection.insertOne(json("a: {b: {c: null}}")); collection.insertOne(json("a: {b: {c: {d: 1}}}")); collection.insertOne(json("a: {b: {c: {d: null}}}")); assertMongoWriteException(() -> collection.insertOne(json("a: {b: {c: 1}}")), 11000, "E11000 duplicate key error collection: testdb.testcoll index: a.b.c_1 dup key: { : 1 }"); assertMongoWriteException(() -> collection.insertOne(json("a: {b: {c: null}}")), 11000, "E11000 duplicate key error collection: testdb.testcoll index: a.b.c_1 dup key: { : null }"); assertMongoWriteException(() -> collection.insertOne(json("a: {b: {c: 1, x: 100}}")), 11000, "E11000 duplicate key error collection: testdb.testcoll index: a.b.c_1 dup key: { : 1 }"); assertMongoWriteException(() -> collection.insertOne(json("a: {b: {c: {d: 1}}}")), 11000, "E11000 duplicate key error collection: testdb.testcoll index: a.b.c_1 dup key: { : { d: 1 } }"); assertMongoWriteException(() -> collection.insertOne(json("a: {b: {c: {d: null}}}")), 11000, "E11000 duplicate key error collection: testdb.testcoll index: a.b.c_1 dup key: { : { d: null } }"); }
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"))); applyIfTrue(options.containsKey("storageEngine"), () -> indexOptions.storageEngine(options.get("storageEngine", Bson.class)));