mongodb spring anno 查询
public interface LogTaskBuilderRepository extends MongoRepository<LogTaskBuilder, String> { @Query(value="{ 'type': {'$in' : ?0} }",fields="{ 'type' : 1}") public List<LogTaskBuilder> findTest(String[] name); @Query(value="{?0:?1}") public List<LogTaskBuilder> findTest2(Object field,Object Value); //正则测试 @Query(value="{ 'type' :{'$regex' :?0, '$options': 'i'} }") public List<LogTaskBuilder> findTest3(String name); //日期查询 @Query(value="{ 'executeDate' :{'$gt' : ?0 } }") public List<LogTaskBuilder> findTest4(Date date); //内部查询 findBy{字段}{条件} public List<LogTaskBuilder> findByTypeLike(String name); public List<LogTaskBuilder> findByTypeLike(String name, Sort sort); public List<LogTaskBuilder> findByTypeLike(String name, Pageable pageable); }
public class LogTaskBuilder { @Id private String name; private RecordType type; private Date executeDate; }
test
System.out.println(logTaskBuilderRepository.findByTypeLike("SoulRecord").size());
        System.out.println(logTaskBuilderRepository.findTest3("Record").size());
        System.out.println(logTaskBuilderRepository.findByTypeLike("Record",new Sort("type", Order.DESCENDING)).size());
        
        System.out.println(logTaskBuilderRepository.findTest(new String[]{"SoulRecord","ItemRecord"}).size());
        System.out.println(logTaskBuilderRepository.findTest2("name","SoulRecord").size());
        
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        
        try {
            System.out.println(logTaskBuilderRepository.findTest4(dateFormat.parse("2012-10-26")).size());
        } catch (ParseException e) {
            e.printStackTrace();
        }
                    
                
                
            
        
浙公网安备 33010602011771号