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

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

Introduction

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

Prototype

UpdateResult upsert(Query query, Update update, String collectionName);

Source Link

Document

Performs an upsert.

Usage

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 w  ww. j av 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);

}