@Test public void testBasicQuery() { BasicDBObject query = new BasicDBObject(); query.put(EmployeeProperties.EMPLOYEE_ID, "28241"); BasicDBObject fields = new BasicDBObject(); fields.put(EmployeeProperties.ID, 0); fields.put(EmployeeProperties.LAST_NAME, 1); fields.put(EmployeeProperties.EMPLOYEE_ID, 1); DBCollection collection = mongoOps.getCollection(EmployeeProperties.COLLECTION); log.info( "MongoDB Query Explain Plan: " + collection .find(query, fields) .hint(new BasicDBObject(EmployeeProperties.EMPLOYEE_ID, 1)) .explain()); DBCursor cursor = collection.find(query, fields).hint(new BasicDBObject(EmployeeProperties.EMPLOYEE_ID, 1)); log.info(cursor.count() + ""); assertNotNull("cursor was null.", cursor); assertEquals("cursor count was not 1.", 1, cursor.count()); log.info("Query (" + query.toString() + ")"); while (cursor.hasNext()) { DBObject dbo = cursor.next(); log.info(dbo.toString()); assertEquals("Keyset size not equal to 2.", 2, dbo.keySet().size()); } log.info(cursor.explain().toString()); }
/** {@link RepositoryOperations#distinct(String, Iterable)} */ public List<Object> distinct(String field, Iterable<QueryCriteria> queryCriterias) { Criteria criteria = MongoQueryUtils.getQueryFromQueryCriteria(queryCriterias); Query query = new Query(); if (criteria != null) { query.addCriteria(criteria); } return mongoOperations .getCollection(mongoOperations.getCollectionName(model)) .distinct(field, query.getQueryObject()); }
/** * Creates an index on the desired field in the target collection. * * @param field * @param direction * @param isUnique * @param isSparse */ public void createIndex( String field, Sort.Direction direction, boolean isUnique, boolean isSparse) { Integer dir = direction.equals(Sort.Direction.ASC) ? 1 : -1; DBObject index = new BasicDBObject(field, dir); DBObject options = new BasicDBObject(); if (isSparse) options.put("sparse", true); if (isUnique) options.put("unique", true); DBCollection collection = mongoOperations.getCollection(mongoOperations.getCollectionName(model)); collection.createIndex(index, options); }
@Before public void setUp() { if (!operations.collectionExists(Customer.class)) { operations.createCollection(Customer.class); } // just make sure we remove everything properly operations.getCollection("system.js").remove(new BasicDBObject()); repository.deleteAll(); }
/** {@link RepositoryOperations#distinct(String)} */ public List<Object> distinct(String field) { return mongoOperations.getCollection(mongoOperations.getCollectionName(model)).distinct(field); }