Example usage for com.mongodb.client.model Updates addToSet

List of usage examples for com.mongodb.client.model Updates addToSet

Introduction

In this page you can find the example usage for com.mongodb.client.model Updates addToSet.

Prototype

public static <TItem> Bson addToSet(final String fieldName, @Nullable final TItem value) 

Source Link

Document

Creates an update that adds the given value to the array value of the field with the given name, unless the value is already present, in which case it does nothing

Usage

From source file:com.maschel.lca.lcacloud.analytics.Analytics.java

License:Open Source License

public void store(AnalyticsDataMessage analyticsData) {
    for (AnalyticsSensorDataDTO sensorData : analyticsData.values) {

        String collectionName = sensorData.name + "_" + sensorData.aggregate;

        Bson filter = Filters.eq("device-id", analyticsData.deviceId);

        if (!database.getCollection(collectionName).find(filter).iterator().hasNext()) {
            database.getCollection(collectionName).insertOne(new Document()
                    .append("device-id", analyticsData.deviceId).append("values", new ArrayList<Document>()));
        }//from  w  ww.j a v a2 s . c om

        database.getCollection(collectionName).updateOne(filter,
                Updates.addToSet("values", new Document(sensorData.date, sensorData.value)));
    }
}