@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()); }
@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()))); }
@Test public void testUpdateWithCollation() { 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"); UpdateOperations<FacebookUser> updateOperations = getDs().createUpdateOperations(FacebookUser.class) .inc("loginCount"); UpdateResults results = getDs().update(query, updateOperations); assertEquals(1, results.getUpdatedCount()); assertEquals(0, getDs().find(FacebookUser.class).filter("id", 1).get().loginCount); assertEquals(1, getDs().find(FacebookUser.class).filter("id", 2).get().loginCount); results = getDs().update(query, updateOperations, new UpdateOptions() .multi(true) .collation(Collation.builder() .locale("en") .collationStrength(CollationStrength.SECONDARY) .build())); assertEquals(2, results.getUpdatedCount()); assertEquals(1, getDs().find(FacebookUser.class).filter("id", 1).get().loginCount); assertEquals(2, getDs().find(FacebookUser.class).filter("id", 2).get().loginCount); }
@Test public void testDeleteWithCollation() { 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"); assertEquals(1, getDs().delete(query).getN()); assertEquals(1, getDs().delete(query, new DeleteOptions() .collation(Collation.builder() .locale("en") .collationStrength(CollationStrength.SECONDARY) .build())) .getN()); }
@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()); }
public String getOperationName(String function) { StringBuilder builder = new StringBuilder(); String collectionName = getCollection().getNamespace().getCollectionName(); builder.append("Mongo : "); builder.append(collectionName); builder.append(" : distinct "); builder.append(fieldName); if (function != null) { builder.append(" : "); builder.append(function); } Bson filter = getFilter(); if (filter != null) { filter = MongoUtilities.filterParameters(filter.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry())); builder.append(" : Filter "); builder.append(filter.toString()); } Collation collation = getCollation(); if (collation != null) { builder.append(" : Collation "); builder.append(collation.asDocument().toString()); } return builder.toString(); }
@Test public void testAggregate() { List<Document> docList = new ArrayList<Document>(); coll.aggregate(Arrays.asList(Aggregates.match(Filters.eq("name", "Alto")), Aggregates.group("color", Accumulators.sum("count", 1)))).into(docList); assertEquals(1, docList.size()); docList.clear(); Document first = coll .aggregate(Arrays.asList(Aggregates.match(Filters.eq("name", "Alto")), Aggregates.group("color", Accumulators.sum("count", 1))), Document.class) .allowDiskUse(true).batchSize(12).bypassDocumentValidation(true).collation(Collation.builder().build()) .first(); Assert.assertNotNull(first); first = coll .aggregate(Arrays.asList(Aggregates.match(Filters.eq("name", "Alto")), Aggregates.group("color", Accumulators.sum("count", 1))), Document.class) .allowDiskUse(true).batchSize(12).bypassDocumentValidation(true).collation(Collation.builder().build()) .map(new Function<Document, Document>() { @Override public Document apply(Document t) { t.put("hello", "world"); return t; } }).first(); Assert.assertNotNull(first); }
@Test public void testFind() { FindIterable<Document> find = coll.find(Filters.eq("name", "Alto"), Document.class) .sort(Sorts.ascending("color")); List<Document> docList = toDocumentList(find); assertEquals(4, docList.size()); find = coll.find(Filters.eq("name", "Alto")).sort(Sorts.ascending("color")); docList = toDocumentList(find); assertEquals(4, docList.size()); find = coll.find(Document.class).filter(Filters.eq("name", "Alto")).sort(Sorts.ascending("color")); docList = toDocumentList(find); assertEquals(4, docList.size()); find = coll.find().filter(Filters.eq("name", "Alto")).sort(Sorts.ascending("color")); docList = toDocumentList(find); assertEquals(4, docList.size()); find = coll.find().filter(Filters.eq("name", "Alto")).sort(Sorts.ascending("color")).batchSize(123) .collation(Collation.builder().build()).cursorType(CursorType.NonTailable).limit(2) .maxAwaitTime(12, TimeUnit.DAYS).maxTime(12, TimeUnit.DAYS).noCursorTimeout(true).oplogReplay(false) .partial(false).skip(1); docList = toDocumentList(find); assertEquals(2, docList.size()); Document firstFind = coll.find().filter(Filters.eq("name", "Alto")).sort(Sorts.ascending("color")).first(); Assert.assertNotNull(firstFind); coll.find().filter(Filters.eq("name", "Alto")).forEach(new Block<Document>() { @Override public void apply(Document t) { System.out.println(t.get("name")); } }); }
/** * 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; }
@Test public void passThrough() { Collation collation = Collation.builder() .locale("en") .caseLevel(true) .build(); DBCollectionRemoveOptions options = new DeleteOptions() .collation(collation) .writeConcern(WriteConcern.JOURNALED) .getOptions(); assertEquals(collation, options.getCollation()); assertEquals(WriteConcern.JOURNALED, options.getWriteConcern()); }
@Test public void testCollation() { checkMinServerVersion(3.4); getDs().save(asList(new Book("The Banquet", "Dante", 2), new Book("Divine Comedy", "Dante", 1), new Book("Eclogues", "Dante", 2), new Book("The Odyssey", "Homer", 10), new Book("Iliad", "Homer", 10))); final String map = "function () { emit(this.author, 1); return; }"; final String reduce = "function (key, values) { return values.length }"; Query<Book> query = getAds().find(Book.class) .field("author").equal("dante"); MapReduceOptions<CountResult> options = new MapReduceOptions<CountResult>() .resultType(CountResult.class) .outputType(OutputType.INLINE) .query(query) .map(map) .reduce(reduce); Iterator<CountResult> iterator = getDs().mapReduce(options).getInlineResults(); Assert.assertEquals(0, count(iterator)); options .inputCollection(getMorphia().getMapper().getCollectionName(Book.class)) .collation(Collation.builder() .locale("en") .collationStrength(CollationStrength.SECONDARY) .build()); iterator = getDs().mapReduce(options).getInlineResults(); CountResult result = iterator.next(); Assert.assertEquals("Dante", result.getAuthor()); Assert.assertEquals(3D, result.getCount(), 0); }
@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()); }
public Collation getCollation() { return collation; }
public String getOperationName(String function) { StringBuilder builder = new StringBuilder(); String collectionName = getCollection().getNamespace().getCollectionName(); builder.append("Mongo : "); builder.append(collectionName); builder.append(" : find"); if( function != null ) { builder.append(" : "); builder.append(function); } Bson filter = getFilter(); if (filter != null) { filter = MongoUtilities.filterParameters(filter.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry())); builder.append(" : Filter "); builder.append(filter.toString()); } Bson sort = getSort(); if (sort != null) { builder.append(" : Sort "); builder.append(sort.toString()); } Bson modifiers = getModifiers(); if (modifiers != null) { builder.append(" : Modifiers "); builder.append(modifiers.toString()); } Bson projection = getProjection(); if (projection != null) { builder.append(" : Projection "); builder.append(projection.toString()); } if (limit != -1) { builder.append(" : Limit "); builder.append(limit); } Collation collation = getCollation(); if (collation != null) { builder.append(" : Collation "); builder.append(collation.asDocument().toString()); } CursorType cursorType2 = getCursorType(); if (cursorType2 != null) { builder.append(" : Cursor Type "); builder.append(cursorType2.toString()); } return builder.toString(); }
public String getOperationName(String function) { StringBuilder builder = new StringBuilder(); String collectionName = getCollection().getNamespace().getCollectionName(); builder.append("Mongo : "); builder.append(collectionName); builder.append(" : mapReduce"); if( function != null ) { builder.append(" : "); builder.append(function); } Bson filter = getFilter(); if (filter != null) { filter = MongoUtilities.filterParameters(filter.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry())); builder.append(" : Filter "); builder.append(filter.toString()); } Bson sort = getSort(); if (sort != null) { builder.append(" : Sort "); builder.append(sort.toString()); } if (limit != -1) { builder.append(" : Limit "); builder.append(limit); } Collation collation = getCollation(); if (collation != null) { builder.append(" : Collation "); builder.append(collation.asDocument().toString()); } return builder.toString(); }
public static IndexOptions toIndexOptions(final Document options) { final IndexOptions indexOptions = new IndexOptions(); applyIfTrue(options.containsKey("background"), () -> indexOptions.background(options.getBoolean("background"))); applyIfTrue(options.containsKey("bits"), () -> indexOptions.bits(options.getInteger("bits"))); applyIfTrue(options.containsKey("bucketSize"), () -> indexOptions.bucketSize(options.getDouble("bucketSize"))); applyIfTrue(options.containsKey("collation"), () -> { 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"))); applyIfTrue(options.containsKey("expireAfterSeconds"), () -> indexOptions.expireAfter(options.getLong("expireAfterSeconds"), TimeUnit.SECONDS)); applyIfTrue(options.containsKey("language_override"), () -> indexOptions.languageOverride(options.getString("language_override"))); applyIfTrue(options.containsKey("max"), () -> indexOptions.max(options.getDouble("max"))); applyIfTrue(options.containsKey("min"), () -> indexOptions.min(options.getDouble("min"))); 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"))); applyIfTrue(options.containsKey("storageEngine"), () -> indexOptions.storageEngine(options.get("storageEngine", Bson.class))); applyIfTrue(options.containsKey("textVersion"), () -> indexOptions.textVersion(options.getInteger("textVersion"))); applyIfTrue(options.containsKey("unique"), () -> indexOptions.unique(options.getBoolean("unique"))); applyIfTrue(options.containsKey("version"), () -> indexOptions.version(options.getInteger("version"))); applyIfTrue(options.containsKey("weights"), () -> indexOptions.weights(options.get("weights", Bson.class))); return indexOptions; }
@Test public void passThrough() { Collation collation = Collation.builder() .locale("en") .caseLevel(true) .build(); DBCollectionFindOptions options = new FindOptions() .batchSize(42) .limit(18) .modifier("i'm a", "modifier") .modifier("i am", 2) .projection(new BasicDBObject("field", "value")) .maxTime(15, TimeUnit.MINUTES) .maxAwaitTime(45, TimeUnit.SECONDS) .skip(12) .sort(new BasicDBObject("field", -1)) .cursorType(CursorType.TailableAwait) .noCursorTimeout(true) .oplogReplay(true) .partial(true) .readPreference(ReadPreference.secondaryPreferred(2, TimeUnit.MINUTES)) .readConcern(ReadConcern.LOCAL) .collation(collation).getOptions(); assertEquals(42, options.getBatchSize()); assertEquals(18, options.getLimit()); assertEquals(new BasicDBObject("i'm a", "modifier") .append("i am", 2), options.getModifiers()); assertEquals(new BasicDBObject("field", "value"), options.getProjection()); assertEquals(15, options.getMaxTime(TimeUnit.MINUTES)); assertEquals(45, options.getMaxAwaitTime(TimeUnit.SECONDS)); assertEquals(12, options.getSkip()); assertEquals(new BasicDBObject("field", -1), options.getSort()); assertEquals(CursorType.TailableAwait, options.getCursorType()); assertTrue(options.isNoCursorTimeout()); assertTrue(options.isOplogReplay()); assertTrue(options.isPartial()); assertEquals(ReadPreference.secondaryPreferred(2, TimeUnit.MINUTES), options.getReadPreference()); assertEquals(ReadConcern.LOCAL, options.getReadConcern()); assertEquals(collation, options.getCollation()); }
@Test @SuppressWarnings("deprecation") public void mapReduceCommand() { Query<FacebookUser> query = getDs().find(FacebookUser.class); MapReduceOptions<FacebookUser> options = new MapReduceOptions<FacebookUser>() .bypassDocumentValidation(true) .collation(Collation.builder().locale("en").build()) .finalize("i'm a finalize function") .jsMode(true) .limit(42) .map("i'm a map function") .maxTimeMS(42000) .outputCollection("output collection") .outputDB("output db") .outputType(OutputType.INLINE) .query(query) .readPreference(ReadPreference.primaryPreferred()) .reduce("i'm a reduce function") .scope(new Document("key", "value").append("key2", "value2")) .verbose(true); MapReduceCommand command = options.toCommand(getMorphia().getMapper()); assertTrue(command.getBypassDocumentValidation()); assertEquals(Collation.builder().locale("en").build(), command.getCollation()); assertTrue(command.getJsMode()); assertEquals(42, command.getLimit()); assertEquals("i'm a map function", command.getMap()); assertEquals(42000, command.getMaxTime(TimeUnit.MILLISECONDS)); assertEquals("output collection", command.getOutputTarget()); assertEquals("output db", command.getOutputDB()); assertEquals(query.getQueryObject(), command.getQuery()); assertEquals(query.getSortObject(), command.getSort()); assertEquals(ReadPreference.primaryPreferred(), command.getReadPreference()); assertEquals("i'm a map function", command.getMap()); assertEquals("i'm a reduce function", command.getReduce()); assertEquals("i'm a finalize function", command.getFinalize()); assertEquals(new Document("key", "value").append("key2", "value2"), command.getScope()); assertTrue(command.isVerbose()); }
@Test public void testFindAndModifyWithOptions() { 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"); UpdateOperations<FacebookUser> updateOperations = getDs().createUpdateOperations(FacebookUser.class) .inc("loginCount"); FacebookUser results = getDs().findAndModify(query, updateOperations, new FindAndModifyOptions()); assertEquals(0, getDs().find(FacebookUser.class).filter("id", 1).get().loginCount); assertEquals(1, getDs().find(FacebookUser.class).filter("id", 2).get().loginCount); assertEquals(1, results.loginCount); results = getDs().findAndModify(query, updateOperations, new FindAndModifyOptions() .returnNew(false) .collation(Collation.builder() .locale("en") .collationStrength(CollationStrength.SECONDARY) .build())); assertEquals(1, getDs().find(FacebookUser.class).filter("id", 1).get().loginCount); assertEquals(0, results.loginCount); assertEquals(1, getDs().find(FacebookUser.class).filter("id", 2).get().loginCount); results = getDs().findAndModify(getDs().find(FacebookUser.class) .field("id").equal(3L) .field("username").equal("Jon Snow"), updateOperations, new FindAndModifyOptions() .returnNew(false) .upsert(true)); assertNull(results); FacebookUser user = getDs().find(FacebookUser.class).filter("id", 3).get(); assertEquals(1, user.loginCount); assertEquals("Jon Snow", user.username); results = getDs().findAndModify(getDs().find(FacebookUser.class) .field("id").equal(4L) .field("username").equal("Ron Swanson"), updateOperations, new FindAndModifyOptions() .upsert(true)); assertNotNull(results); user = getDs().find(FacebookUser.class).filter("id", 4).get(); assertEquals(1, results.loginCount); assertEquals("Ron Swanson", results.username); assertEquals(1, user.loginCount); assertEquals("Ron Swanson", user.username); }
/** * Returns the collation options * * @return the collation options * @mongodb.server.release 3.4 */ public Collation getCollation() { return options.getCollation(); }