Search in sources :

Example 6 with Update

use of org.springframework.data.mongodb.core.query.Update in project spring-boot-examples by ityouknow.

the class UserDaoImpl method updateUser.

/**
 * 更新对象
 * @param user
 */
@Override
public int updateUser(UserEntity user) {
    Query query = new Query(Criteria.where("id").is(user.getId()));
    Update update = new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord());
    // 更新查询返回结果集的第一条
    WriteResult result = mongoTemplate.updateFirst(query, update, UserEntity.class);
    // mongoTemplate.updateMulti(query,update,UserEntity.class);
    if (result != null)
        return result.getN();
    else
        return 0;
}
Also used : WriteResult(com.mongodb.WriteResult) Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update)

Example 7 with Update

use of org.springframework.data.mongodb.core.query.Update in project spring-integration by spring-projects.

the class MongoDbMessageStore method getNextId.

private int getNextId() {
    Query query = Query.query(Criteria.where("_id").is(SEQUENCE_NAME));
    query.fields().include(SEQUENCE);
    return (Integer) this.template.findAndModify(query, new Update().inc(SEQUENCE, 1), FindAndModifyOptions.options().returnNew(true).upsert(true), Map.class, this.collectionName).get(SEQUENCE);
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update)

Example 8 with Update

use of org.springframework.data.mongodb.core.query.Update in project Spring-Family by Sierou-Java.

the class UserServiceImpl method updateUser.

/**
 * 更新对象
 *
 * @param user
 */
@Override
public int updateUser(UserEntity user) {
    Query query = new Query(Criteria.where("id").is(user.getId()));
    Update update = new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord());
    // 更新查询返回结果集的第一条
    WriteResult result = mongoTemplate.updateFirst(query, update, UserEntity.class);
    // mongoTemplate.updateMulti(query,update,UserEntity.class);
    return result != null ? result.getN() : 0;
}
Also used : WriteResult(com.mongodb.WriteResult) Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update)

Example 9 with Update

use of org.springframework.data.mongodb.core.query.Update in project newton by muoncore.

the class MongoSagaRepository method updateSagaCreated.

private void updateSagaCreated(Saga saga, NewtonEvent event) {
    Query query = new Query();
    query.addCriteria(Criteria.where("sagaId").is(saga.getId()));
    Update update = new Update();
    update.set("sagaClassName", saga.getClass().getName());
    update.set("sagaId", saga.getId());
    update.set("eventId", event.getId());
    mongoTemplate.upsert(query, update, SagaCreated.class);
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update)

Example 10 with Update

use of org.springframework.data.mongodb.core.query.Update in project cas by apereo.

the class MongoDbWebAuthnCredentialRepository method update.

@Override
@SneakyThrows
protected void update(final String username, final Collection<CredentialRegistration> givenRecords) {
    val records = givenRecords.stream().map(record -> {
        if (record.getRegistrationTime() == null) {
            return record.withRegistrationTime(Instant.now(Clock.systemUTC()));
        }
        return record;
    }).collect(Collectors.toList());
    val query = new Query(Criteria.where(MongoDbWebAuthnCredentialRegistration.FIELD_USERNAME).is(username)).collation(Collation.of(Locale.ENGLISH).strength(Collation.ComparisonLevel.primary()));
    val collection = getProperties().getAuthn().getMfa().getWebAuthn().getMongo().getCollection();
    if (records.isEmpty()) {
        LOGGER.debug("No records are provided for [{}] so entry will be removed", username);
        mongoTemplate.remove(query, MongoDbWebAuthnCredentialRegistration.class, collection);
    } else {
        val jsonRecords = getCipherExecutor().encode(WebAuthnUtils.getObjectMapper().writeValueAsString(records));
        val entry = MongoDbWebAuthnCredentialRegistration.builder().records(jsonRecords).username(username).build();
        val update = Update.update(MongoDbWebAuthnCredentialRegistration.FIELD_RECORDS, jsonRecords);
        val result = mongoTemplate.updateFirst(query, update, collection);
        if (result.getMatchedCount() <= 0) {
            LOGGER.debug("Storing new registration record for [{}]", username);
            mongoTemplate.save(entry, collection);
        }
    }
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Unchecked(org.jooq.lambda.Unchecked) SneakyThrows(lombok.SneakyThrows) CipherExecutor(org.apereo.cas.util.crypto.CipherExecutor) Collection(java.util.Collection) lombok.val(lombok.val) CredentialRegistration(com.yubico.data.CredentialRegistration) Set(java.util.Set) BaseWebAuthnCredentialRepository(org.apereo.cas.webauthn.storage.BaseWebAuthnCredentialRepository) Collation(org.springframework.data.mongodb.core.query.Collation) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Locale(java.util.Locale) MongoOperations(org.springframework.data.mongodb.core.MongoOperations) Update(org.springframework.data.mongodb.core.query.Update) Clock(java.time.Clock) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Query(org.springframework.data.mongodb.core.query.Query) SneakyThrows(lombok.SneakyThrows)

Aggregations

Update (org.springframework.data.mongodb.core.query.Update)212 Test (org.junit.jupiter.api.Test)179 Document (org.bson.Document)117 Query (org.springframework.data.mongodb.core.query.Query)72 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)57 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)23 Bson (org.bson.conversions.Bson)21 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)15 UpdateResult (com.mongodb.client.result.UpdateResult)12 ObjectId (org.bson.types.ObjectId)12 List (java.util.List)8 ToString (lombok.ToString)8 AggregationUpdate (org.springframework.data.mongodb.core.aggregation.AggregationUpdate)8 Criteria (org.springframework.data.mongodb.core.query.Criteria)8 VersionedPerson (org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson)7 StepVerifier (reactor.test.StepVerifier)7 BsonDocument (org.bson.BsonDocument)6 UpdateOptions (com.mongodb.client.model.UpdateOptions)5 ArrayList (java.util.ArrayList)5 Instant (java.time.Instant)4