List of usage examples for com.mongodb.client.model ReplaceOneModel getOptions
@Deprecated
public UpdateOptions getOptions()
From source file:net.es.netshell.mongodb.MongoDBProvider.java
License:Open Source License
@Override public final void store(List<ResourceAnchor> anchors) throws IOException { if (!KernelThread.currentKernelThread().isPrivileged()) { throw new SecurityException("store db list - not authorized"); }/*from w ww.j a v a 2s.c o m*/ HashMap<String, ArrayList<WriteModel>> collectionRequests = new HashMap<String, ArrayList<WriteModel>>(); // Build the bulk requests per collection for (ResourceAnchor anchor : anchors) { String user = anchor.getContainerOwner(); String collection = anchor.getContainerName(); String collectionName = user + "_" + collection; ArrayList<WriteModel> requests; if (!collectionRequests.containsKey(collectionName)) { requests = new ArrayList<WriteModel>(); collectionRequests.put(collectionName, requests); } else { requests = collectionRequests.get(collectionName); } try { // Likely to be in the Resource cache. Otherwise replace by itself. Resource resource = Resource.findByName(user, collection, anchor.getResourceName()); Document doc = Document.parse(resource.saveToJSON()); Document query = new Document("resourceName", resource.getResourceName()); ; ReplaceOneModel<Document> request = new ReplaceOneModel<Document>(query, doc); request.getOptions().upsert(true); requests.add(request); } catch (InstantiationException e) { throw new IOException(e); } } // Bulk write the collection's request for (Map.Entry<String, ArrayList<WriteModel>> entry : collectionRequests.entrySet()) { String[] name = entry.getKey().split("_"); ArrayList<WriteModel> requests = entry.getValue(); MongoCollection mongoCollection = this.getCollection(name[0], name[1]); if (mongoCollection == null) { throw new RuntimeErrorException(new Error("Could not store into collection " + entry.getKey())); } BulkWriteOptions options = new BulkWriteOptions(); options.ordered(false); mongoCollection.bulkWrite(requests, options); } }