Examples of MongoTemplate


Examples of org.springframework.data.document.mongodb.MongoTemplate

  private final MongoTemplate mongoDbTemplate;

  @Autowired
  private MongoPersonRepository(Mongo mongo) {
    Assert.notNull(mongo, "Mongo is required");
    mongoDbTemplate = new MongoTemplate(mongo, "test", new SimpleMongoConverter());
  }
View Full Code Here

Examples of org.springframework.data.mongodb.core.MongoTemplate

*/
public class DemoUtils {

  public static MongoDbFactory prepareMongoFactory(String... additionalCollectionToDrop) throws Exception{
    MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(new MongoClient(), "test");
    MongoTemplate template = new MongoTemplate(mongoDbFactory);
    template.dropCollection("messages");
    template.dropCollection("data");
    for (String additionalCollection : additionalCollectionToDrop) {
      template.dropCollection(additionalCollection);
    }
    return mongoDbFactory;
  }
View Full Code Here

Examples of org.springframework.data.mongodb.core.MongoTemplate

public class SpringMongoDBConverter implements MongoDBConverter {

    private MongoTemplate mongoTemplate;

    public SpringMongoDBConverter(Mongo mongo, String dbname) {
        this.mongoTemplate = new MongoTemplate(mongo, dbname);
    }
View Full Code Here

Examples of org.springframework.data.mongodb.core.MongoTemplate

public class MongoApp2 {
  private static final Log log = LogFactory.getLog(MongoApp.class);

  public static void main(String[] args) throws Exception {
    MongoOperations mongoOps = new MongoTemplate(new SimpleMongoDbFactory(
        new Mongo(), "database"));
    Person p = new Person("Joe", 34);
    // Insert is used to initially store the object into the database.
    mongoOps.insert(p);
    log.info("Insert: " + p); // Find
    p = mongoOps.findById(p.getId(), Person.class);
    log.info("Found: " + p); // Update
    mongoOps.updateFirst(query(where("name").is("Joe")), update("age", 35),
        Person.class);
    p = mongoOps.findOne(query(where("name").is("Joe")), Person.class);
    log.info("Updated: " + p); // Delete mongoOps.remove(p);
    // Check that deletion worked
    List<Person> people = mongoOps.findAll(Person.class);
    log.info("Number of people = : " + people.size());
//    mongoOps.dropCollection(Person.class);
  }
View Full Code Here

Examples of org.springframework.data.mongodb.core.MongoTemplate

public class MongoApp {
  private static final Log log = LogFactory.getLog(MongoApp.class);

  public static void main(String[] args) throws Exception {
    MongoOperations mongoOps = new MongoTemplate(new Mongo(), "database");
    mongoOps.insert(new Person("Joe", 34));
    log.info(mongoOps.findOne(new Query(where("name").is("Joe")),
        Person.class));
//    mongoOps.dropCollection("person");
  }
View Full Code Here

Examples of org.springframework.data.mongodb.core.MongoTemplate

  @RequestMapping(value = "/omArea/delete", method = { RequestMethod.POST })
  public void delete(HttpServletResponse response, Integer[] ids)
      throws IOException {

    MongoTemplate template = omAreaService.getOmAreaDao()
        .getMongoTemplate();
    Query q = new Query();
    List list = Arrays.asList(ids);
    q.addCriteria(where("areaId").in(list));
    template.remove(q, OmAreaBean.class);
    response.setContentType("text/json;charset=utf-8");
    response.getWriter().write("{\"success\":true}");
  }
View Full Code Here

Examples of org.springframework.data.mongodb.core.MongoTemplate

    public MongoDbFactory mongoDbFactory() {
      return new SimpleMongoDbFactory(mongo, "impromptu-db");
    }
   
    public @Bean MongoTemplate mongoTemplate() {
      return new MongoTemplate(mongoDbFactory());
  }
View Full Code Here

Examples of org.springframework.data.mongodb.core.MongoTemplate

    createService(VCAP_MONGO_SERVICE, "mongodb", "1.8");
    int tunnelPort = LOCAL_PORT + 2;
    createTunnelServer(VCAP_MONGO_SERVICE, tunnelPort);
    Mongo mongo = new Mongo(LOCAL_HOST, tunnelPort);
    MongoDbFactory mdbf = new SimpleMongoDbFactory(mongo, svc_dbname, new UserCredentials(svc_username, svc_passwd));
    MongoTemplate mongoTemplate = new MongoTemplate(mdbf);
    // Test data
    ObjectMapper objectMapper = new ObjectMapper();
    Map<String,Object> dataMap = null;
    try {
      dataMap = objectMapper.readValue(new File("data/load.json"), Map.class);
    } catch (IOException e) {
      e.printStackTrace();
    }
    List<Map<String, Object>> l = (List<Map<String, Object>>) dataMap.get("records");
    for (Map<String, Object> m : l) {
      Map<String, Object> rec = (Map<String, Object>) m.get("record");
      final BasicDBObject dbo = new BasicDBObject();
      dbo.putAll(rec);
      mongoTemplate.execute("records", new CollectionCallback<Object>() {
        public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
          collection.insert(dbo);
          return null;
        }
      });
    }
    List records = mongoTemplate.findAll(BasicDBObject.class, "records");
    Assert.assertEquals("Did not load the data correctly", 200, records.size());
    mongo.close();

    stopTunnelServer();
    removeService(VCAP_MONGO_SERVICE);
View Full Code Here

Examples of org.springframework.data.mongodb.core.MongoTemplate

    return new SimpleMongoDbFactory(new MongoClient("localhost"), "spring-data-rest");
  }

  @Bean
  public MongoTemplate mongoTemplate() throws UnknownHostException {
    return new MongoTemplate(mongoDbFactory());
  }
View Full Code Here

Examples of org.springframework.data.mongodb.core.MongoTemplate

  public void invokesOverriddenDeleteMethodCorrectly() {

    MyRepo repository = mock(MyRepo.class);

    MongoRepositoryFactoryBean<MyRepo, Domain, ObjectId> factory = new MongoRepositoryFactoryBean<MyRepo, Domain, ObjectId>();
    factory.setMongoOperations(new MongoTemplate(mock(MongoDbFactory.class)));
    factory.setRepositoryInterface(MyRepo.class);
    factory.setLazyInit(true);
    factory.afterPropertiesSet();

    ObjectId id = new ObjectId();
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.