Search in sources :

Example 1 with ID_FIELD

use of org.jnosql.diana.mongodb.document.MongoDBUtils.ID_FIELD in project jnosql-diana-driver by eclipse.

the class MongoDBDocumentCollectionManagerAsync method update.

private void update(DocumentEntity entity, SingleResultCallback<Document> callBack) {
    String collectionName = entity.getName();
    com.mongodb.async.client.MongoCollection<Document> asyncCollection = asyncMongoDatabase.getCollection(collectionName);
    Document id = entity.find(ID_FIELD).map(d -> new Document(d.getName(), d.getValue().get())).orElseThrow(() -> new UnsupportedOperationException("To update this DocumentEntity " + "the field `_id` is required"));
    asyncCollection.findOneAndReplace(id, getDocument(entity), callBack);
}
Also used : FindIterable(com.mongodb.async.client.FindIterable) Document(org.bson.Document) MongoDBUtils.getDocument(org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) DocumentCollectionManagerAsync(org.jnosql.diana.api.document.DocumentCollectionManagerAsync) MongoCollection(com.mongodb.async.client.MongoCollection) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) BsonDocument(org.bson.BsonDocument) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Bson(org.bson.conversions.Bson) Consumer(java.util.function.Consumer) List(java.util.List) Documents(org.jnosql.diana.api.document.Documents) Block(com.mongodb.Block) MongoDatabase(com.mongodb.async.client.MongoDatabase) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Duration(java.time.Duration) DeleteResult(com.mongodb.client.result.DeleteResult) ID_FIELD(org.jnosql.diana.mongodb.document.MongoDBUtils.ID_FIELD) SingleResultCallback(com.mongodb.async.SingleResultCallback) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Document(org.bson.Document) MongoDBUtils.getDocument(org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument) BsonDocument(org.bson.BsonDocument)

Example 2 with ID_FIELD

use of org.jnosql.diana.mongodb.document.MongoDBUtils.ID_FIELD in project jnosql-diana-driver by eclipse.

the class MongoDBDocumentCollectionManager method update.

@Override
public DocumentEntity update(DocumentEntity entity) {
    DocumentEntity copy = entity.copy();
    String collectionName = entity.getName();
    MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
    Document id = copy.find(ID_FIELD).map(d -> new Document(d.getName(), d.getValue().get())).orElseThrow(() -> new UnsupportedOperationException("To update this DocumentEntity " + "the field `id` is required"));
    copy.remove(ID_FIELD);
    collection.findOneAndReplace(id, getDocument(entity));
    return entity;
}
Also used : Document(org.bson.Document) MongoDBUtils.getDocument(org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument) MongoCollection(com.mongodb.client.MongoCollection) MongoDatabase(com.mongodb.client.MongoDatabase) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) BsonDocument(org.bson.BsonDocument) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Bson(org.bson.conversions.Bson) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) DocumentCollectionManager(org.jnosql.diana.api.document.DocumentCollectionManager) Documents(org.jnosql.diana.api.document.Documents) StreamSupport.stream(java.util.stream.StreamSupport.stream) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Duration(java.time.Duration) DeleteResult(com.mongodb.client.result.DeleteResult) ID_FIELD(org.jnosql.diana.mongodb.document.MongoDBUtils.ID_FIELD) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.bson.Document) MongoDBUtils.getDocument(org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument) BsonDocument(org.bson.BsonDocument)

Aggregations

DeleteResult (com.mongodb.client.result.DeleteResult)2 Duration (java.time.Duration)2 List (java.util.List)2 BsonDocument (org.bson.BsonDocument)2 Document (org.bson.Document)2 Bson (org.bson.conversions.Bson)2 DocumentDeleteQuery (org.jnosql.diana.api.document.DocumentDeleteQuery)2 DocumentEntity (org.jnosql.diana.api.document.DocumentEntity)2 DocumentQuery (org.jnosql.diana.api.document.DocumentQuery)2 Documents (org.jnosql.diana.api.document.Documents)2 ID_FIELD (org.jnosql.diana.mongodb.document.MongoDBUtils.ID_FIELD)2 MongoDBUtils.getDocument (org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument)2 Block (com.mongodb.Block)1 SingleResultCallback (com.mongodb.async.SingleResultCallback)1 FindIterable (com.mongodb.async.client.FindIterable)1 MongoCollection (com.mongodb.async.client.MongoCollection)1 MongoDatabase (com.mongodb.async.client.MongoDatabase)1 MongoCollection (com.mongodb.client.MongoCollection)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1