@Override public long totalCount() { return collection(IndexFailureImpl.class).count(); } }
protected long totalCount(String collectionName) { return collection(collectionName).count(); }
protected <T extends Persisted> long totalCount(Class<T> modelClass) { return collection(modelClass).count(); }
protected long count(DBObject query, String collectionName) { return collection(collectionName).count(query); }
protected <T extends Persisted> long count(Class<T> modelClass, DBObject query) { return collection(modelClass).count(query); }
@Override public long getCount(final String collection) { return getCollection(collection).count(); }
/** * Only calls DBCollection.count() when using vanilla MongoDB; otherwise gets estimate from collection.getStats() */ private String safeCount(DBCollection collection, Class<? extends Timestamp> type) { if (type.equals(Timestamp.BSON.class)) { return "" + collection.count(); // Vanilla MongoDB can quickly return precise count } CommandResult stats = collection.getStats(); return "~" + (!stats.containsField("count") ? 0l : stats.getLong("count")); }
@Override public long fetchCount() { try { Predicate filter = createFilter(queryMixin.getMetadata()); return collection.count(createQuery(filter)); } catch (NoResults ex) { return 0L; } }
/** * Creates a GridFS instance for the specified bucket in the given database. Set the preferred WriteConcern on the give DB with * DB.setWriteConcern * * @param db database to work with * @param bucket bucket to use in the given database * @throws com.mongodb.MongoException if there's a failure * @see com.mongodb.WriteConcern */ public GridFS(final DB db, final String bucket) { this.database = db; this.bucketName = bucket; this.filesCollection = database.getCollection(bucketName + ".files"); this.chunksCollection = database.getCollection(bucketName + ".chunks"); // ensure standard indexes as long as collections are small try { if (filesCollection.count() < 1000) { filesCollection.createIndex(new BasicDBObject("filename", 1).append("uploadDate", 1)); } if (chunksCollection.count() < 1000) { chunksCollection.createIndex(new BasicDBObject("files_id", 1).append("n", 1), new BasicDBObject("unique", true)); } } catch (MongoException e) { //TODO: Logging } filesCollection.setObjectClass(GridFSDBFile.class); }
public void installSampleData() { File file = new File("zips.json"); try { if (!file.exists()) { file = new File(System.getProperty("java.io.tmpdir"), "zips.json"); if (!file.exists()) { download(new URL("http://media.mongodb.org/zips.json"), file); } } DBCollection zips = getDb().getCollection("zips"); if (zips.count() == 0) { new ProcessExecutor().command(MONGO_IMPORT, "--db", getDb().getName(), "--collection", "zipcodes", "--file", file.getAbsolutePath()) .redirectError(System.err) .execute(); } } catch (Exception e) { e.printStackTrace(); } Assume.assumeTrue("Failed to process media files", file.exists()); }
/** * Real test */ @Test public void testSavingBook() throws Exception { // Test saving getDs().save(newBook()); assertEquals(1, getDs().getCollection(Book.class).count()); }
@Test public void testBulkInsertWithNullWC() { this.getAds().insert(asList(new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity()), new InsertOptions()); Assert.assertEquals(5, getDs().getCollection(TestEntity.class).count()); String name = "some_collection"; this.getAds().insert(name, asList(new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity()), new InsertOptions()); Assert.assertEquals(5, getMongoClient().getDatabase(TEST_DB_NAME).getCollection(name).count()); }
@Test @SuppressWarnings("deprecation") public void testCommentsShowUpInLogsOld() { getDs().save(asList(new Pic("pic1"), new Pic("pic2"), new Pic("pic3"), new Pic("pic4"))); getDb().command(new BasicDBObject("profile", 2)); String expectedComment = "test comment"; toList(getDs().find(Pic.class).comment(expectedComment).find()); DBCollection profileCollection = getDb().getCollection("system.profile"); assertNotEquals(0, profileCollection.count()); DBObject profileRecord = profileCollection.findOne(new BasicDBObject("op", "query") .append("ns", getDs().getCollection(Pic.class).getFullName())); assertEquals(profileRecord.toString(), expectedComment, getCommentFromProfileRecord(profileRecord)); turnOffProfilingAndDropProfileCollection(); }
@Test public void testCommentsShowUpInLogs() { getDs().save(asList(new Pic("pic1"), new Pic("pic2"), new Pic("pic3"), new Pic("pic4"))); getDb().command(new BasicDBObject("profile", 2)); String expectedComment = "test comment"; toList(getDs().find(Pic.class) .find(new FindOptions() .modifier("$comment", expectedComment))); DBCollection profileCollection = getDb().getCollection("system.profile"); assertNotEquals(0, profileCollection.count()); DBObject profileRecord = profileCollection.findOne(new BasicDBObject("op", "query") .append("ns", getDs().getCollection(Pic.class).getFullName())); assertEquals(profileRecord.toString(), expectedComment, getCommentFromProfileRecord(profileRecord)); turnOffProfilingAndDropProfileCollection(); }
@Test @SuppressWarnings("deprecation") public void testBulkInsertOld() { this.getAds().insert(asList(new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity()), new InsertOptions().writeConcern(WriteConcern.ACKNOWLEDGED)); Assert.assertEquals(5, getDs().getCollection(TestEntity.class).count()); String name = "some_collection"; MongoCollection<Document> collection = getMongoClient().getDatabase(TEST_DB_NAME).getCollection(name); this.getAds().insert(name, asList(new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity()), new InsertOptions().writeConcern(WriteConcern.ACKNOWLEDGED)); Assert.assertEquals(5, collection.count()); collection.drop(); this.getAds().insert(name, asList(new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity()), WriteConcern.ACKNOWLEDGED); Assert.assertEquals(5, collection.count()); }
@Test public void testOut() { checkMinServerVersion(2.6); 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))); AggregationOptions options = builder() .build(); Iterator<Author> aggregate = getDs().createAggregation(Book.class) .group("author", grouping("books", push("title"))) .out(Author.class, options); Assert.assertEquals(2, getDs().getCollection(Author.class).count()); Author author = aggregate.next(); Assert.assertEquals("Homer", author.name); Assert.assertEquals(asList("The Odyssey", "Iliad"), author.books); getDs().createAggregation(Book.class) .group("author", grouping("books", push("title"))) .out("different", Author.class); Assert.assertEquals(2, getDb().getCollection("different").count()); }
@Test public void testBulkInsert() { this.getAds().insert(asList(new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity()), new InsertOptions().writeConcern(WriteConcern.ACKNOWLEDGED)); Assert.assertEquals(5, getDs().getCollection(TestEntity.class).count()); String name = "some_collection"; MongoCollection<Document> collection = getMongoClient().getDatabase(TEST_DB_NAME).getCollection(name); this.getAds().insert(name, asList(new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity()), new InsertOptions().writeConcern(WriteConcern.ACKNOWLEDGED)); Assert.assertEquals(5, collection.count()); collection.drop(); this.getAds().insert(name, asList(new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity()), new InsertOptions() .writeConcern(WriteConcern.ACKNOWLEDGED)); Assert.assertEquals(5, collection.count()); }
@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()); }
@Override protected int getCountInternal(Query query) throws IOException { Filter f = query.getFilter(); if (isAll(f)) { LOG.fine("count(all)"); return (int) collection.count(); } Filter[] split = splitFilter(f); if (!isAll(split[1])) { return -1; } DBObject q = toQuery(f); if (LOG.isLoggable(Level.FINE)) { LOG.fine("count(" + q + ")"); } return (int) collection.count(q); }
DBCursor cursor = null; if (isCountQuery) return dbCollection.count(mongoQuery); else cursor = orderBy != null ? dbCollection.find(mongoQuery, keys).sort(orderBy).limit(maxResult)