All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anwen.mongo.toolkit.DocumentUpdater Maven / Gradle / Ivy

There is a newer version: 2.1.6.1
Show newest version
package com.anwen.mongo.toolkit;

import org.bson.Document;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author JiaChaoYang
 * @project mongo-plus
 * @description
 * @date 2023-11-24 13:36
 **/
public class DocumentUpdater {

    private final AtomicReference docRef;

    public DocumentUpdater(Document doc) {
        this.docRef = new AtomicReference<>(doc);
    }

    public Document update(String key, Object value) {
        Document oldDoc, newDoc;
        do {
            oldDoc = docRef.get();
            newDoc = new Document(oldDoc);
            newDoc.put(key, value);
        } while (!docRef.compareAndSet(oldDoc, newDoc));
        return newDoc;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy