From a4ab7b9cfed765fd8c17d57647f6d38ffd5158d2 Mon Sep 17 00:00:00 2001 From: Marc Date: Sat, 13 Mar 2021 19:48:19 +0100 Subject: [PATCH] [mongodb] Collection per Item (#10202) Put Item in separate Collections Signed-off-by: Marc --- .../org.openhab.persistence.mongodb/README.md | 7 ++- .../internal/MongoDBPersistenceService.java | 61 ++++++++++++++++--- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/bundles/org.openhab.persistence.mongodb/README.md b/bundles/org.openhab.persistence.mongodb/README.md index 23c55c5360cd6..51a352d26a0b0 100644 --- a/bundles/org.openhab.persistence.mongodb/README.md +++ b/bundles/org.openhab.persistence.mongodb/README.md @@ -11,6 +11,9 @@ This service can be configured in the file `services/mongodb.cfg`. | ---------- | ------- | :------: | ---------------------------------------------------------------------------- | | url | | Yes | connection URL to address MongoDB. For example, `mongodb://localhost:27017` | | database | | Yes | database name | -| collection | | Yes | collection name | +| collection | | Yes | set collection to "" if it shall generate a collection per item | -All item and event related configuration is done in the file `persistence/mongodb.persist`. +If you have a username and password it looks like this: url = mongodb://[username]:[password]@[localhost]:27017/[database] +The database is required: http://mongodb.github.io/mongo-java-driver/3.9/javadoc/com/mongodb/MongoClientURI.html + +All item and event related configuration is done in the file `persistence/mongodb.persist`. \ No newline at end of file diff --git a/bundles/org.openhab.persistence.mongodb/src/main/java/org/openhab/persistence/mongodb/internal/MongoDBPersistenceService.java b/bundles/org.openhab.persistence.mongodb/src/main/java/org/openhab/persistence/mongodb/internal/MongoDBPersistenceService.java index 6eb29f648accc..9d23d45d88e65 100644 --- a/bundles/org.openhab.persistence.mongodb/src/main/java/org/openhab/persistence/mongodb/internal/MongoDBPersistenceService.java +++ b/bundles/org.openhab.persistence.mongodb/src/main/java/org/openhab/persistence/mongodb/internal/MongoDBPersistenceService.java @@ -87,6 +87,7 @@ public class MongoDBPersistenceService implements QueryablePersistenceService { private @NonNullByDefault({}) String url; private @NonNullByDefault({}) String db; private @NonNullByDefault({}) String collection; + private boolean collectionPerItem; private boolean initialized = false; @@ -117,9 +118,9 @@ public void activate(final BundleContext bundleContext, final Map query(FilterCriteria filter) { } String name = filter.getItemName(); + + // If collection Per Item is active, connect to the item Collection + if (collectionPerItem) { + connectToCollection(name); + } Item item = getItem(name); List items = new ArrayList<>(); @@ -315,6 +358,10 @@ public Iterable query(FilterCriteria filter) { ZonedDateTime.ofInstant(obj.getDate(FIELD_TIMESTAMP).toInstant(), ZoneId.systemDefault()))); } + // If collection Per Item is active, disconnect after save. + if (collectionPerItem) { + disconnectFromCollection(); + } return items; }