@Test @SuppressWarnings("deprecation") public void testCorrectQueryForNotWithSizeEqIssue514() { Query<PhotoWithKeywords> query = getAds() .find(PhotoWithKeywords.class) .field("keywords").not().sizeEq(3); assertEquals(new BasicDBObject("keywords", new BasicDBObject("$not", new BasicDBObject("$size", 3))), query.getQueryObject()); }
protected Query<?> buildQuery() { final Query<?> query; if (getCollection() == null) { query = getDatastore().find(getMappedClass().getClazz()); } else { query = ((AdvancedDatastore) getDatastore()).find(getCollection(), getMappedClass().getClazz()); } return query; }
@Test public void testDBObjectOrQuery() { getDs().save(new PhotoWithKeywords("scott", "hernandez")); final List<DBObject> orList = new ArrayList<DBObject>(); orList.add(new BasicDBObject("keywords.keyword", "scott")); orList.add(new BasicDBObject("keywords.keyword", "ralph")); final BasicDBObject orQuery = new BasicDBObject("$or", orList); Query<PhotoWithKeywords> q = getAds().createQuery(PhotoWithKeywords.class, orQuery); assertEquals(1, q.count()); q = getAds().find(PhotoWithKeywords.class).disableValidation().filter("$or", orList); assertEquals(1, q.count()); }
@Test public void testDeleteWithAnEntityTypeAndId() { // given final String ns = "someCollectionName"; getDb().getCollection(ns).remove(new BasicDBObject()); final Rectangle rect = new Rectangle(10, 10); ObjectId id = new ObjectId(); rect.setId(id); getAds().save(ns, rect); assertEquals(1, getAds().find(ns, Rectangle.class).count()); // when getAds().delete(getAds().find(ns, Rectangle.class).filter("_id", id)); // then assertEquals(0, getAds().find(ns, Rectangle.class).count()); }
@Test public void setReferenceWithCollection() { final Author author = new Author("Jane Austen"); getAds().save("jane", author); Set<Book> set = new HashSet<Book>(5); set.add(new Book("Sense and Sensibility")); set.add(new Book("Pride and Prejudice")); set.add(new Book("Mansfield Park")); set.add(new Book("Emma")); set.add(new Book("Northanger Abbey")); for (final Book book : set) { book.setAuthor("jane", author); getAds().save("books", book); } author.setSet("books", set); getAds().save("jane", author); final Author loaded = getAds().find("jane", Author.class).filter("_id", author.getId()).first(); Assert.assertFalse(loaded.set.isResolved()); final Set<Book> set1 = loaded.getSet(); assertEquals(set.size(), set1.size()); for (final Book book : set) { assertTrue("Looking for " + book + " in " + set1, set1.contains(book)); } assertTrue(loaded.set.isResolved()); }
@Test public void testDeleteWillRemoveAnyDocumentWithAMatchingId() { // given final String ns = "someCollectionName"; getDb().getCollection(ns).remove(new BasicDBObject()); final Rectangle rect = new Rectangle(10, 10); ObjectId rectangleId = new ObjectId(); rect.setId(rectangleId); getAds().save(ns, rect); final Circle circle = new Circle(); circle.setId(new ObjectId()); getAds().save(ns, circle); assertEquals(2, getAds().find(ns, Rectangle.class).count()); // when getAds().delete(getAds().find(ns, Circle.class).filter("_id", rectangleId)); // then assertEquals(1, getAds().find(ns, Circle.class).count()); }
@Test public void testGet() { final String ns = "hotels"; final Rectangle rect = new Rectangle(10, 10); getDb().getCollection(ns).remove(new BasicDBObject()); getAds().save(ns, rect); assertEquals(1, getAds().find(ns, Rectangle.class).count()); final Rectangle rectLoaded = getAds().find(ns, Rectangle.class) .filter("_id", rect.getId()) .first(); assertEquals(rect.getId(), rectLoaded.getId()); assertEquals(rect.getArea(), rectLoaded.getArea(), 0); } }
@Test public void listReferenceWithCollection() { final Author author = new Author("Jane Austen"); getAds().save("jane", author); List<Book> list = new ArrayList<Book>(); list.add(new Book("Sense and Sensibility")); list.add(new Book("Pride and Prejudice")); list.add(new Book("Mansfield Park")); list.add(new Book("Emma")); list.add(new Book("Northanger Abbey")); for (final Book book : list) { book.setAuthor("jane", author); getAds().save("books", book); } author.setList("books", list); getAds().save("jane", author); Assert.assertNull(getDs().find(Author.class).first()); Assert.assertNotNull(getAds().find("jane", Author.class).first()); Assert.assertNull(getDs().find(Book.class).first()); Assert.assertNotNull(getAds().find("books", Book.class).first()); final Author loaded = getAds().find("jane", Author.class).filter("_id", author.getId()).first(); Assert.assertFalse(loaded.list.isResolved()); Assert.assertEquals(list, loaded.getList()); assertTrue(loaded.list.isResolved()); }
@Test public void testDeleteDoesNotDeleteAnythingWhenGivenAnIncorrectId() { // given final String ns = "someCollectionName"; getDb().getCollection(ns).remove(new BasicDBObject()); final Rectangle rect = new Rectangle(10, 10); ObjectId id = new ObjectId(); rect.setId(id); getAds().save(ns, rect); assertEquals(1, getAds().find(ns, Rectangle.class).count()); // when giving an ID that is not the entity ID. Note that at the time of writing this will also log a validation warning getAds().delete(getAds().find(ns, Rectangle.class).filter("_id", 1)); // then assertEquals(1, getDb().getCollection(ns).count()); }
@Test public void testFluentNotQuery() { final PhotoWithKeywords pwk = new PhotoWithKeywords("scott", "hernandez"); getDs().save(pwk); final Query<PhotoWithKeywords> query = getAds().find(PhotoWithKeywords.class); query.criteria("keywords.keyword").not().startsWith("ralph"); assertEquals(1, query.count()); }
@Test @SuppressWarnings("deprecation") public void testOldMapReduce() { final Random rnd = new Random(); //create 100 circles and rectangles for (int i = 0; i < 100; i++) { getAds().insert("shapes", new Circle(rnd.nextDouble())); getAds().insert("shapes", new Rectangle(rnd.nextDouble(), rnd.nextDouble())); } final String map = "function () { if(this['radius']) { emit('circle', {count:1}); return; } emit('rect', {count:1}); }"; final String reduce = "function (key, values) { var total = 0; for ( var i=0; i<values.length; i++ ) {total += values[i].count;} " + "return { count : total }; }"; final MapreduceResults<ResultEntity> mrRes = getDs().mapReduce(MapreduceType.REPLACE, getAds().find(Shape.class), map, reduce, null, null, ResultEntity.class); Assert.assertEquals(2, mrRes.createQuery().countAll()); Assert.assertEquals(100, mrRes.createQuery() .find(new FindOptions().limit(1)) .tryNext() .getValue().count, 0); final MapreduceResults<ResultEntity> inline = getDs().mapReduce(MapreduceType.INLINE, getAds().find(Shape.class), map, reduce, null, null, ResultEntity.class); final Iterator<ResultEntity> iterator = inline.iterator(); Assert.assertEquals(2, count(iterator)); Assert.assertEquals(100, inline.iterator().next().getValue().count, 0); }
@Test(expected = MongoException.class) public void testBadMR() { final String map = "function () { if(this['radius']) { doEmit('circle', {count:1}); return; } emit('rect', {count:1}); }"; final String reduce = "function (key, values) { var total = 0; for ( var i=0; i<values.length; i++ ) {total += values[i].count;} " + "return { count : total }; }"; getDs().mapReduce(new MapReduceOptions<ResultEntity>() .resultType(ResultEntity.class) .outputType(OutputType.REPLACE) .query(getAds().find(Shape.class)) .map(map) .reduce(reduce)); }
@Test @SuppressWarnings("deprecation") public void testFluentAndQuery1() { getDs().save(new PhotoWithKeywords("scott", "hernandez")); final Query<PhotoWithKeywords> q = getAds().find(PhotoWithKeywords.class); q.and(q.criteria("keywords.keyword").hasThisOne("scott"), q.criteria("keywords.keyword").hasAnyOf(asList("scott", "hernandez"))); assertEquals(1, q.count()); assertTrue(q.getQueryObject().containsField("$and")); }
@Test public void testFluentOrQuery() { final PhotoWithKeywords pwk = new PhotoWithKeywords("scott", "hernandez"); getDs().save(pwk); final Query<PhotoWithKeywords> q = getAds().find(PhotoWithKeywords.class); q.or( q.criteria("keywords.keyword").equal("scott"), q.criteria("keywords.keyword").equal("ralph")); assertEquals(1, q.count()); }
@Test public void testBypassDocumentValidation() { checkMinServerVersion(3.2); getDs().save(asList(new User("john doe", new Date()), new User("John Doe", new Date()))); MongoDatabase database = getMongoClient().getDatabase(TEST_DB_NAME); database.getCollection("out_users").drop(); database.createCollection("out_users", new CreateCollectionOptions() .validationOptions(new ValidationOptions() .validator(Document.parse("{ \"age\" : { \"gte\" : 13 } }")))); try { getDs() .createAggregation(User.class) .match(getDs().find(User.class).field("name").equal("john doe")) .out("out_users", User.class); fail("Document validation should have complained."); } catch (MongoCommandException e) { // expected } getDs() .createAggregation(User.class) .match(getDs().find(User.class).field("name").equal("john doe")) .out("out_users", User.class, builder() .bypassDocumentValidation(true) .build()); Assert.assertEquals(1, getAds().find("out_users", User.class).count()); }
@Test public void mapReferenceWithCollection() { final Author author = new Author("Jane Austen"); getAds().save("jane", author); Map<String, Book> books = new LinkedHashMap<String, Book>(); for (final Book book : new Book[]{ new Book("Sense and Sensibility"), new Book("Pride and Prejudice"), new Book("Mansfield Park"), new Book("Emma"), new Book("Northanger Abbey")}) { book.setAuthor("jane", author); getAds().save("books", book); books.put(book.name, book); } author.setMap("books", books); getAds().save("jane", author); Assert.assertNull(getDs().find(Author.class).first()); Assert.assertNotNull(getAds().find("jane", Author.class).first()); Assert.assertNull(getDs().find(Book.class).first()); Assert.assertNotNull(getAds().find("books", Book.class).first()); final Author loaded = getAds().find("jane", Author.class).filter("_id", author.getId()).first(); Assert.assertFalse(loaded.map.isResolved()); Assert.assertEquals(books, loaded.getMap()); assertTrue(loaded.map.isResolved()); }
getDs().mapReduce(new MapReduceOptions<ResultEntity>() .outputType(OutputType.REPLACE) .query(getAds().find(Shape.class)) .map(map) .reduce(reduce) getDs().mapReduce(new MapReduceOptions<ResultEntity>() .outputType(OutputType.INLINE) .query(getAds().find(Shape.class)).map(map).reduce(reduce) .resultType(ResultEntity.class)); final Iterator<ResultEntity> iterator = inline.iterator();
@Test @SuppressWarnings("deprecation") public void testFluentAndOrQuery() { getDs().save(new PhotoWithKeywords("scott", "hernandez")); final Query<PhotoWithKeywords> q = getAds().find(PhotoWithKeywords.class); q.and( q.or(q.criteria("keywords.keyword").equal("scott")), q.or(q.criteria("keywords.keyword").equal("hernandez"))); assertEquals(1, q.count()); assertTrue(q.getQueryObject().containsField("$and")); }
@Test public void basicReferenceWithCollection() { final Author author = new Author("Jane Austen"); getAds().save("jane", author); final Book book = new Book("Pride and Prejudice"); book.setAuthor("jane", author); getDs().save(book); Assert.assertNull(getDs().find(Author.class).first()); Assert.assertNotNull(getAds().find("jane", Author.class).first()); final Book loaded = getDs().find(Book.class).filter("_id", book.id).first(); Assert.assertFalse(loaded.author.isResolved()); Assert.assertEquals(author, loaded.author.get()); assertTrue(loaded.author.isResolved()); }
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>()