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

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

Introduction

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

Prototype

boolean upsert

To view the source code for org.springframework.data.mongodb.core FindAndModifyOptions upsert.

Click Source Link

Usage

From source file:eu.trentorise.game.managers.DBPlayerManager.java

private StatePersistence persist(String gameId, String playerId, List<GenericObjectPersistence> concepts,
        CustomData customData, Map<String, Object> metadata) {
    if (StringUtils.isBlank(gameId) || StringUtils.isBlank(playerId)) {
        throw new IllegalArgumentException("field gameId and playerId of PlayerState MUST be set");
    }/*w  w w.  jav  a  2  s.  c om*/

    Criteria criteria = new Criteria();
    criteria = criteria.and("gameId").is(gameId).and("playerId").is(playerId);
    Query query = new Query(criteria);
    Update update = new Update();
    if (concepts != null) {
        update.set("concepts", concepts);
    }
    if (customData != null) {
        update.set("customData", customData);
    }
    if (metadata != null) {
        update.set("metadata", metadata);
    }
    FindAndModifyOptions options = new FindAndModifyOptions();
    options.upsert(true);
    options.returnNew(true);
    return mongoTemplate.findAndModify(query, update, options, StatePersistence.class);
}