Example usage for org.springframework.data.mongodb.core MongoOperations find

List of usage examples for org.springframework.data.mongodb.core MongoOperations find

Introduction

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

Prototype

<T> List<T> find(Query query, Class<T> entityClass);

Source Link

Document

Map the results of an ad-hoc query on the collection for the entity class to a List of the specified type.

Usage

From source file:core.App.java

public static void main(String[] args) {

    // For XML/*from w  w w .j  a va 2 s .c  om*/
    //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());*/
}