Example usage for org.springframework.data.mongodb.core.query Update Update

List of usage examples for org.springframework.data.mongodb.core.query Update Update

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.query Update Update.

Prototype

Update

Source Link

Usage

From source file:tetrad.rrd.TestSpringMongo.java

/**
 * @param args//from w w  w  . j av  a  2 s . c o  m
 */
public static void main(String[] args) {
    String[] configLocations = new String[] { "applicationContext_rrd.xml" };
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configLocations);

    Operations operations = (Operations) context.getBean("operations");

    Query query = new Query(Criteria.where("key").is("1"));

    Update update = new Update();
    update.set("id", "A");

    WriteResult wr = operations.updateMulti(query, update, "test", true);
    System.out.println(wr);
}

From source file:core.App.java

public static void main(String[] args) {

    // For XML//  w w w.  ja  v a2s.c  o m
    //ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");
    // For Annotation
    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
    MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");

    //Student user = new Student("3", "federico", "solterman", "26-10-1990");
    // save
    //mongoOperation.save(user);
    // now user object got the created id.
    // System.out.println("1. user : " + user);
    // query to search user
    // Query searchUserQuery = new Query(Criteria.where("first_name").is("federico")) {
    //};
    // find the saved user again.
    //Student savedUser = mongoOperation.findOne(searchUserQuery, Student.class);
    // System.out.println("2. find - savedUser : " + savedUser);
    //This method  Fetch all students whose notes in a specific course were greater than 4
    BasicQuery query = new BasicQuery(
            "{ $and: [{ note_1: {$gt:4 }, note_2: {$gt:4}, note_3: {$gt:4},note_final:{$gt:4  } } ] }");

    List<StudentXCourseXNote> stu = mongoOperation.find(query, StudentXCourseXNote.class);
    StudentXCourseXNote aux;

    for (int i = 0; i < stu.size(); i++) {
        aux = stu.get(i);
        BasicQuery query1 = new BasicQuery("{id_registration:" + " \"" + aux.getId_student() + "\"}");

        Student student = mongoOperation.findOne(query1, Student.class);

        System.out.println(student.toString());

    }
    //Fetch all courses ordered by name for a given teacher
    BasicQuery query2 = new BasicQuery("{ last_name:\"Sulma\" }");

    Teacher teacher = mongoOperation.findOne(query2, Teacher.class);

    BasicQuery query3 = new BasicQuery("{  id_teacher: \"" + teacher.getId_teacher() + "\"}");

    List<TeacherXCourse> list = mongoOperation.find(query3, TeacherXCourse.class);

    TeacherXCourse aux2;

    for (int j = 0; j < list.size(); j++) {

        aux2 = list.get(j);

        BasicQuery query4 = new BasicQuery("{id_course: \"" + aux2.getId_course() + "\" }");

        Course course = mongoOperation.findOne(query4, Course.class);

        System.out.println(course.toString());

    }
    //This method add a new fields in the collection course
    BasicQuery queryPoint4 = new BasicQuery("{ },{ $set: { finish: boolean } },{ multi: true }");
    Update update = new Update();
    update.set("{}", "");
    mongoOperation.updateFirst(queryPoint4, null, Course.class);

    // update password
    /*mongoOperation.updateFirst(searchUserQuery,
     Update.update("password", "new password"), Student.class);*/
    // find the updated user object
    /*Student updatedUser = mongoOperation.findOne(searchUserQuery, Student.class);*/
    //System.out.println("3. updatedUser : " + updatedUser);
    // delete
    // mongoOperation.remove(searchUserQuery, Student.class);
    // List, it should be empty now.
    /* List<Student> listUser = mongoOperation.findAll(Student.class);
      System.out.println("4. Number of user = " + listUser.size());*/
}

From source file:com.malsolo.mongodb.humongous.main.Main.java

public static void main(String... args) {

    ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);

    MongoOperations ops = context.getBean("mongoTemplate", MongoOperations.class);

    //Create article
    /*/*from ww  w . j  a v a 2s .c o m*/
    Article article = new Article();
    article.setAuthorId(UUID.randomUUID());
    article.setAuthor("Javier");
    article.setDate(new Date());
    article.setTitle("Ttulo");
            
    //Inserts
    ops.insert(article);
    */

    //Find one article
    Article article = ops.findOne(query(where("author").is("Javier")), Article.class);

    System.out.println(article);

    ArticleRepository articleRepository = context.getBean("articleRepository", ArticleRepository.class);
    article = articleRepository.findByAuthor("Javier");

    System.out.println(article);

    Comment comment = new Comment();
    comment.setAuthor("David el Gnomo");
    comment.setDate(new Date());
    comment.setText("Another comment");

    ops.upsert(query(where("author").is("Javier")), new Update().push("comments", comment), Article.class);

}

From source file:ts.security.PersistanceTokenDaoImpl.java

public void updateToken(String series, String tokenValue, Date lastUsed) {
    Update update = new Update();
    update.set("tokenValue", tokenValue);
    update.set("date", lastUsed);
    Query query = new Query();
    query.addCriteria(Criteria.where("series").is(series));
    mongoTemplate.updateFirst(query, update, "rememberMeTokens");
}

From source file:com.epam.ta.reportportal.database.search.UpdateStatisticsQueryBuilder.java

public static Update fromItemStatusAware(final Status status, final int totalCounter, final int statusCounter) {
    Update updateStatusAware = new Update().inc(EXECUTION_COUNTER + ".total", totalCounter);
    Status providedStatus = status != null ? status : Status.FAILED;
    if (providedStatus.awareStatisticsField() != null && !providedStatus.awareStatisticsField().isEmpty()) {
        updateStatusAware = updateStatusAware.inc(EXECUTION_COUNTER + "." + status.awareStatisticsField(),
                statusCounter);//from   www. j  a v  a  2  s . co m
    }
    return updateStatusAware;
}

From source file:edu.fing.tagsi.mongodb.services.PackageTrackingService.java

public void Add(PackageInfo pi, PackageNode pn) {

    if (pi != null && pn != null) {
        Query query = new Query();
        //_id = idPaquete
        query.addCriteria(Criteria.where("_id").is(pi.getIdPaquete()).and("idCliente").is(pi.getIdCliente()));
        Update update = new Update();
        update.push("nodes", pn);

        mongoOperations.upsert(query, update, PackageInfo.class);
    }//from   ww w . ja va 2 s.c  o  m
}

From source file:quanlyhocvu.api.mongodb.DAO.ChiTietChuyenMonDAO.java

public boolean update(ChiTietChuyenMonDTO dto) {
    Query query = Query.query(Criteria.where("id").is(dto.getid()));
    Update update = new Update();
    update.set("giaoVien", dto.getGiaoVien().getid());
    update.set("mota", dto.getMota());

    mongoOperation.findAndModify(query, update, ChiTietChuyenMonDTO.class);
    return true;//ww w  .j  a v a2 s . c  o  m
}

From source file:quanlyhocvu.api.mongodb.DAO.ChiTietMonHocDAO.java

public boolean update(ChiTietMonHocDTO dto) {
    Query query = Query.query(Criteria.where("id").is(dto.getid()));
    Update update = new Update();
    update.set("monHoc", dto.getMonHoc().getid());
    update.set("khoiLop", dto.getKhoiLop().getid());

    mongoOperation.findAndModify(query, update, ChiTietMonHocDTO.class);
    return true;/*from w w w.  ja  v  a 2  s.  c  om*/
}

From source file:quanlyhocvu.api.mongodb.DAO.ChiTietPhanCongDAO.java

public boolean update(ChiTietPhanCongDTO dto) {
    Query query = Query.query(Criteria.where("id").is(dto.getid()));
    Update update = new Update();
    update.set("phanCong", dto.getPhanCong().getid());
    update.set("thoiGian", dto.getThoiGian());

    mongoOperation.findAndModify(query, update, ChiTietPhanCongDTO.class);
    return true;//  ww  w  . j a  v  a  2 s . c  o m
}

From source file:quanlyhocvu.api.mongodb.DAO.PhanCongDAO.java

public boolean update(PhanCongDTO dto) {
    Query query = Query.query(Criteria.where("id").is(dto.getid()));
    Update update = new Update();
    update.set("chiTietChuyenMon", dto.getChiTietChuyenMon().getid());
    update.set("lopHoc", dto.getLopHoc().getid());

    mongoOperation.findAndModify(query, update, PhanCongDTO.class);
    return true;// ww  w. j  ava2s . c  o m
}