throw new IllegalArgumentException("collation 'locale' should be a String"); } else { builder.locale((String) locale); throw new IllegalArgumentException("collation 'caseLevel' should be a Boolean"); } else { builder.caseLevel((Boolean) caseLevel); throw new IllegalArgumentException("collation 'caseFirst' should be a String"); } else { builder.collationCaseFirst(CollationCaseFirst.fromString((String) caseFirst)); throw new IllegalArgumentException("collation 'strength' should be an Integer"); } else { builder.collationStrength(CollationStrength.fromInt((Integer) strength)); throw new IllegalArgumentException("collation 'numericOrdering' should be a Boolean"); } else { builder.numericOrdering((Boolean) numericOrdering); throw new IllegalArgumentException("collation 'alternate' should be a String"); } else { builder.collationAlternate(CollationAlternate.fromString((String) alternate)); throw new IllegalArgumentException("collation 'maxVariable' should be a String"); } else { builder.collationMaxVariable(CollationMaxVariable.fromString((String) maxVariable)); throw new IllegalArgumentException("collation 'normalization' should be a Boolean");
switch (collationKey) { case "locale": builder.locale(collationObj); break; case "caseLevel": builder.caseLevel(Boolean.parseBoolean(collationObj)); break; case "caseFirst": builder.collationCaseFirst(CollationCaseFirst.fromString(collationObj)); break; case "strength": builder.collationStrength(CollationStrength.valueOf(collationObj)); break; case "numericOrdering": builder.numericOrdering(Boolean.parseBoolean(collationObj)); break; case "normalization": builder.normalization(Boolean.parseBoolean(collationObj)); break; case "backwards": builder.backwards(Boolean.parseBoolean(collationObj)); break; case "alternate": builder.collationAlternate(CollationAlternate.fromString(collationObj)); break; case "maxVariable": builder.collationMaxVariable(CollationMaxVariable.fromString(collationObj)); break; default:
throw new IllegalArgumentException("collation 'locale' should be a String"); } else { builder.locale((String) locale); throw new IllegalArgumentException("collation 'caseLevel' should be a Boolean"); } else { builder.caseLevel((Boolean) caseLevel); throw new IllegalArgumentException("collation 'caseFirst' should be a String"); } else { builder.collationCaseFirst(CollationCaseFirst.fromString((String) caseFirst)); throw new IllegalArgumentException("collation 'strength' should be an Integer"); } else { builder.collationStrength(CollationStrength.fromInt((Integer) strength)); throw new IllegalArgumentException("collation 'numericOrdering' should be a Boolean"); } else { builder.numericOrdering((Boolean) numericOrdering); throw new IllegalArgumentException("collation 'alternate' should be a String"); } else { builder.collationAlternate(CollationAlternate.fromString((String) alternate)); throw new IllegalArgumentException("collation 'maxVariable' should be a String"); } else { builder.collationMaxVariable(CollationMaxVariable.fromString((String) maxVariable)); throw new IllegalArgumentException("collation 'normalization' should be a Boolean");
final Document collationData = options.get("collation", Document.class); final Collation.Builder builder = Collation.builder(); applyIfTrue(collationData.containsKey("backwards"), () -> builder.backwards(collationData.getBoolean("backwards"))); applyIfTrue(collationData.containsKey("caseLevel"), () -> builder.caseLevel(collationData.getBoolean("caseLevel"))); applyIfTrue(collationData.containsKey("alternate"), () -> builder.collationAlternate(CollationAlternate.fromString(collationData.getString("alternate")))); applyIfTrue(collationData.containsKey("caseFirst"), () -> builder.collationCaseFirst(CollationCaseFirst.fromString(collationData.getString("caseFirst")))); applyIfTrue(collationData.containsKey("maxVariable"), () -> builder.collationMaxVariable(CollationMaxVariable.fromString(collationData.getString("maxVariable")))); applyIfTrue(collationData.containsKey("strength"), () -> builder.collationStrength(CollationStrength.fromInt(collationData.getInteger("strength")))); applyIfTrue(collationData.containsKey("locale"), () -> builder.locale(collationData.getString("locale"))); applyIfTrue(collationData.containsKey("normalization"), () -> builder.normalization(collationData.getBoolean("normalization"))); applyIfTrue(collationData.containsKey("numericOrdering"), () -> builder.numericOrdering(collationData.getBoolean("numericOrdering"))); indexOptions.collation(builder.build()); }); applyIfTrue(options.containsKey("default_language"), () -> indexOptions.defaultLanguage(options.getString("default_language")));
private static Collation getCollation(Document dbObject) { Collation collation = null; if ( dbObject != null ) { String caseFirst = dbObject.getString( "caseFirst" ); Integer strength = dbObject.getInteger( "strength" ); String alternate = dbObject.getString( "alternate" ); String maxVariable = dbObject.getString( "maxVariable" ); collation = Collation.builder() .locale( (String) dbObject.get( "locale" ) ) .caseLevel( (Boolean) dbObject.get( "caseLevel" ) ) .numericOrdering( (Boolean) dbObject.get( "numericOrdering" ) ) .backwards( (Boolean) dbObject.get( "backwards" ) ) .collationCaseFirst( caseFirst == null ? null : CollationCaseFirst.fromString( caseFirst ) ) .collationStrength( strength == null ? null : CollationStrength.fromInt( strength ) ) .collationAlternate( alternate == null ? null : CollationAlternate.fromString( alternate ) ) .collationMaxVariable( maxVariable == null ? null : CollationMaxVariable.fromString( maxVariable ) ) .build(); } return collation; }
private static Collation getCollation(Document dbObject) { Collation collation = null; if ( dbObject != null ) { String caseFirst = dbObject.getString( "caseFirst" ); Integer strength = dbObject.getInteger( "strength" ); String alternate = dbObject.getString( "alternate" ); String maxVariable = dbObject.getString( "maxVariable" ); collation = Collation.builder() .locale( (String) dbObject.get( "locale" ) ) .caseLevel( (Boolean) dbObject.get( "caseLevel" ) ) .numericOrdering( (Boolean) dbObject.get( "numericOrdering" ) ) .backwards( (Boolean) dbObject.get( "backwards" ) ) .collationCaseFirst( caseFirst == null ? null : CollationCaseFirst.fromString( caseFirst ) ) .collationStrength( strength == null ? null : CollationStrength.fromInt( strength ) ) .collationAlternate( alternate == null ? null : CollationAlternate.fromString( alternate ) ) .collationMaxVariable( maxVariable == null ? null : CollationMaxVariable.fromString( maxVariable ) ) .build(); } return collation; }
/** * Copies this instance to a new one. * * @return the new instance */ public DeleteOptions copy() { DeleteOptions deleteOptions = new DeleteOptions() .writeConcern(getWriteConcern()); if (getCollation() != null) { deleteOptions.collation(Collation.builder(getCollation()).build()); } return deleteOptions; }
com.mongodb.client.model.Collation convert(final Collation collation) { return com.mongodb.client.model.Collation.builder() .locale(collation.locale()) .backwards(collation.backwards()) .caseLevel(collation.caseLevel()) .collationAlternate(collation.alternate()) .collationCaseFirst(collation.caseFirst()) .collationMaxVariable(collation.maxVariable()) .collationStrength(collation.strength()) .normalization(collation.normalization()) .numericOrdering(collation.numericOrdering()) .build(); }
builder.locale(source.locale.asString()); builder.collationStrength(CollationStrength.fromInt(strength.getLevel())); strength.getCaseFirst().ifPresent(it -> builder.collationCaseFirst(CollationCaseFirst.fromString(it.state))); }); source.alternate.ifPresent(it -> { builder.collationAlternate(CollationAlternate.fromString(it.alternate)); it.maxVariable .ifPresent(maxVariable -> builder.collationMaxVariable(CollationMaxVariable.fromString(maxVariable))); }); source.normalization.ifPresent(builder::normalization); return builder.build(); };
.returnNew(false) .collation(Collation.builder() .locale("en") .collationStrength(CollationStrength.SECONDARY) .build())); assertEquals(1, getDs().find(FacebookUser.class).filter("id", 1) .find(new FindOptions().limit(1))
MapReduceOptions<FacebookUser> options = new MapReduceOptions<FacebookUser>() .bypassDocumentValidation(true) .collation(Collation.builder().locale("en").build()) .finalize("i'm a finalize function") .jsMode(true) assertEquals(Collation.builder().locale("en").build(), command.getCollation()); assertTrue(command.getJsMode()); assertEquals(42, command.getLimit());
.multi(true) .collation(Collation.builder() .locale("en") .collationStrength(CollationStrength.SECONDARY) .build())); assertEquals(2, results.getUpdatedCount()); assertEquals(1, getDs().find(FacebookUser.class).filter("id", 1)
@Test public void passThrough() { Collation collation = Collation.builder() .locale("en") .caseLevel(true) .build(); DBCollectionFindAndModifyOptions options = new FindAndModifyOptions() .bypassDocumentValidation(true) .collation(collation).getOptions() .maxTime(15, TimeUnit.MINUTES) .projection(new BasicDBObject("field", "value")) .remove(true) .returnNew(true) .sort(new BasicDBObject("field", -1)) .update(new BasicDBObject("$inc", "somefield")) .upsert(true) .writeConcern(WriteConcern.JOURNALED); assertTrue(options.getBypassDocumentValidation()); assertEquals(collation, options.getCollation()); assertEquals(15, options.getMaxTime(TimeUnit.MINUTES)); assertEquals(new BasicDBObject("field", "value"), options.getProjection()); assertTrue(options.isRemove()); assertTrue(options.returnNew()); assertEquals(new BasicDBObject("field", -1), options.getSort()); assertEquals(new BasicDBObject("$inc", "somefield"), options.getUpdate()); assertTrue(options.isUpsert()); assertEquals(WriteConcern.JOURNALED, options.getWriteConcern()); } }
.inputCollection(getMorphia().getMapper().getCollectionName(Book.class)) .collation(Collation.builder() .locale("en") .collationStrength(CollationStrength.SECONDARY) .build()); iterator = getDs().mapReduce(options).getInlineResults(); CountResult result = iterator.next();
@Test public void testCollations() { checkMinServerVersion(3.4); getMorphia().map(ContainsRenamedFields.class); getDs().save(asList(new ContainsRenamedFields("first", "last"), new ContainsRenamedFields("First", "Last"))); Query query = getDs().find(ContainsRenamedFields.class) .field("last_name").equal("last"); assertEquals(1, toList(query.find()).size()); assertEquals(2, toList(query.find(new FindOptions() .collation(builder() .locale("en") .collationStrength(CollationStrength.SECONDARY) .build()))) .size()); assertEquals(1, query.count()); assertEquals(2, query.count(new CountOptions() .collation(builder() .locale("en") .collationStrength(CollationStrength.SECONDARY) .build()))); }
@Test public void passThrough() { Collation collation = Collation.builder() .locale("en") .caseLevel(true) .build(); DBCollectionCountOptions options = new CountOptions() .collation(collation) .hint("i'm a hint") .limit(18) .maxTime(15, TimeUnit.MINUTES) .readPreference(ReadPreference.secondaryPreferred()) .readConcern(ReadConcern.LOCAL) .skip(12) .getOptions(); assertEquals(collation, options.getCollation()); assertEquals("i'm a hint", options.getHintString()); assertEquals(18, options.getLimit()); assertEquals(15, options.getMaxTime(TimeUnit.MINUTES)); assertEquals(ReadPreference.secondaryPreferred(), options.getReadPreference()); assertEquals(ReadConcern.LOCAL, options.getReadConcern()); assertEquals(12, options.getSkip()); } }
builder.locale(source.locale.asString()); builder.collationStrength(CollationStrength.fromInt(strength.getLevel())); strength.getCaseFirst().ifPresent(it -> builder.collationCaseFirst(CollationCaseFirst.fromString(it.state))); }); source.alternate.ifPresent(it -> { builder.collationAlternate(CollationAlternate.fromString(it.alternate)); it.maxVariable .ifPresent(maxVariable -> builder.collationMaxVariable(CollationMaxVariable.fromString(maxVariable))); }); source.normalization.ifPresent(builder::normalization); return builder.build(); };
@Test public void testFindAndDeleteWithCollation() { checkMinServerVersion(3.4); getDs().getCollection(FacebookUser.class).drop(); getDs().save(asList(new FacebookUser(1, "John Doe"), new FacebookUser(2, "john doe"))); Query<FacebookUser> query = getDs().find(FacebookUser.class) .field("username").equal("john doe"); assertNotNull(getDs().findAndDelete(query)); assertNull(getDs().findAndDelete(query)); FindAndModifyOptions options = new FindAndModifyOptions() .collation(Collation.builder() .locale("en") .collationStrength(CollationStrength.SECONDARY) .build()); assertNotNull(getDs().findAndDelete(query, options)); assertTrue("Options should not be modified by the datastore", options.isReturnNew()); assertFalse("Options should not be modified by the datastore", options.isRemove()); }
@Test public void testCollation() { checkMinServerVersion(3.4); getDs().save(asList(new User("john doe", new Date()), new User("John Doe", new Date()))); Query query = getDs().find(User.class).field("name").equal("john doe"); AggregationPipeline pipeline = getDs() .createAggregation(User.class) .match(query); Assert.assertEquals(1, count(pipeline.aggregate(User.class))); Assert.assertEquals(2, count(pipeline.aggregate(User.class, builder() .collation(Collation.builder() .locale("en") .collationStrength( CollationStrength.SECONDARY) .build()).build()))); }