/src/main/java/br/com/igormaldonado/springbootexample/mongo/PageDao.java

https://bitbucket.org/ifloor/kafkaboot · Java · 46 lines · 33 code · 9 blank · 4 comment · 0 complexity · d2b4b02c48dccf60277567106deee937 MD5 · raw file

  1. package br.com.igormaldonado.springbootexample.mongo;
  2. import br.com.igormaldonado.springbootexample.model.Page;
  3. import com.mongodb.bulk.BulkWriteResult;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.geo.Circle;
  6. import org.springframework.data.geo.Distance;
  7. import org.springframework.data.geo.Point;
  8. import org.springframework.data.mongodb.core.BulkOperations;
  9. import org.springframework.data.mongodb.core.MongoTemplate;
  10. import org.springframework.data.mongodb.core.query.Criteria;
  11. import org.springframework.data.mongodb.core.query.Query;
  12. import org.springframework.stereotype.Component;
  13. import java.util.List;
  14. @Component
  15. public class PageDao {
  16. @Autowired
  17. private PageRepository pageRepository;
  18. @Autowired
  19. private MongoTemplate mongoTemplate;
  20. public void bulkInsert(List<Page> pages) {
  21. BulkOperations bulkOperations = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, Page.class);
  22. BulkWriteResult result = bulkOperations.insert(pages).execute();
  23. System.out.println("result from bulk: "+result.toString());
  24. }
  25. public List<Page> findClose(Point center, Distance distance) {
  26. System.out.println("center: "+center.getX()+" - "+center.getY());
  27. System.out.println("Distance value: "+ distance.getValue());
  28. System.out.println("unit: "+distance.getUnit());
  29. Circle circle = new Circle(center, distance);
  30. Query query = Query.query(Criteria.where("location").withinSphere(circle));
  31. //
  32. //
  33. // return mongoTemplate.find(query, Page.class);
  34. return mongoTemplate.find(query, Page.class);
  35. // return pageRepository.findByLocationNear(center, distance);
  36. }
  37. }