/** {@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); }
private ExecutableMongoScript createExecutablePutIfAbsentScript(Customer customer) { String collectionName = operations.getCollectionName(Customer.class); Object id = operations .getConverter() .getMappingContext() .getPersistentEntity(Customer.class) .getIdentifierAccessor(customer) .getIdentifier(); DBObject dbo = new BasicDBObject(); operations.getConverter().write(customer, dbo); String scriptString = String.format( "object = db.%1$s.findOne('{\"_id\": \"%2$s\"}'); if (object == null) { db.%1s.insert(%3$s); return null; } else { return object; }", collectionName, id, dbo); return new ExecutableMongoScript(scriptString); }
/** {@link RepositoryOperations#distinct(String)} */ public List<Object> distinct(String field) { return mongoOperations.getCollection(mongoOperations.getCollectionName(model)).distinct(field); }