List of usage examples for com.mongodb.client.model UpdateOptions upsert
boolean upsert
To view the source code for com.mongodb.client.model UpdateOptions upsert.
Click Source Link
From source file:com.qwazr.connectors.MongoDbConnector.java
License:Apache License
/** * Create a new UpdateOptions object/*from ww w . j a v a 2 s.co m*/ * * @param upsert true if a new document should be inserted if there are no * matches to the query filter * @return a new UpdateOptions object */ @JsonIgnore public UpdateOptions getNewUpdateOptions(boolean upsert) { UpdateOptions updateOptions = new UpdateOptions(); updateOptions.upsert(upsert); return updateOptions; }
From source file:com.qwazr.library.mongodb.MongoDbConnector.java
License:Apache License
/** * Create a new UpdateOptions object//from ww w .ja va 2 s . c o m * * @param upsert true if a new document should be inserted if there are no * matches to the query filter * @return a new UpdateOptions object */ @JsonIgnore public UpdateOptions getNewUpdateOptions(final boolean upsert) { UpdateOptions updateOptions = new UpdateOptions(); updateOptions.upsert(upsert); return updateOptions; }
From source file:joliex.mongodb.MongoDbConnector.java
@RequestResponse public Value updateMany(Value request) throws FaultException { try {/*from ww w. ja v a2 s . c om*/ String collectionName = request.getFirstChild("collection").strValue(); Value v = Value.create(); BsonDocument bsonQueryDocument = BsonDocument.parse(request.getFirstChild("filter").strValue()); prepareBsonQueryData(bsonQueryDocument, request.getFirstChild("filter")); printlnJson("Update filter", bsonQueryDocument); BsonDocument bsonDocument = BsonDocument.parse(request.getFirstChild("documentUpdate").strValue()); printlnJson("Update documentUpdate", bsonDocument); prepareBsonQueryData(bsonDocument, request.getFirstChild("documentUpdate")); printlnJson("Update documentUpdate", bsonDocument); if (request.hasChildren("writeConcern")) { WriteConcern writeConcern = new WriteConcern(); if (request.getFirstChild("writeConcern").hasChildren("journal")) { writeConcern.withJournal( request.getFirstChild("writeConcern").getFirstChild("journal").boolValue()); } if (request.getFirstChild("writeConcern").hasChildren("w")) { if (request.getFirstChild("writeConcern").getFirstChild("w").isInt()) { writeConcern.withW(request.getFirstChild("writeConcern").getFirstChild("w").intValue()); } if (request.getFirstChild("writeConcern").getFirstChild("w").isString()) { writeConcern.withW(request.getFirstChild("writeConcern").getFirstChild("w").strValue()); } } if (request.getFirstChild("writeConcern").hasChildren("timeout")) { writeConcern.withWTimeout( request.getFirstChild("writeConcern").getFirstChild("timeout").longValue(), TimeUnit.MILLISECONDS); } db.getCollection(collectionName, BsonDocument.class).withWriteConcern(writeConcern); } if (request.hasChildren("options")) { UpdateOptions updateOptions = new UpdateOptions(); updateOptions.upsert(request.getFirstChild("options").getFirstChild("upsert").boolValue()); updateOptions.bypassDocumentValidation( request.getFirstChild("options").getFirstChild("bypassDocumentValidation").boolValue()); UpdateResult resultUpdate = db.getCollection(collectionName, BsonDocument.class) .updateMany(bsonQueryDocument, bsonDocument, updateOptions); v.getNewChild("matchedCount").assignValue(Value.create(resultUpdate.getMatchedCount())); v.getNewChild("modifiedCount").assignValue(Value.create(resultUpdate.getModifiedCount())); } else { UpdateResult resultUpdate = db.getCollection(collectionName, BsonDocument.class) .updateMany(bsonQueryDocument, bsonDocument); v.getNewChild("matchedCount").assignValue(Value.create(resultUpdate.getMatchedCount())); v.getNewChild("modifiedCount").assignValue(Value.create(resultUpdate.getModifiedCount())); } return v; } catch (MongoException ex) { throw new FaultException("MongoException", ex); } catch (JsonParseException ex) { throw new FaultException("JsonParseException", ex); } }
From source file:joliex.mongodb.MongoDbConnector.java
@RequestResponse public Value update(Value request) throws FaultException { try {//from ww w .j a v a 2 s . co m Value v = Value.create(); String collectionName = request.getFirstChild("collection").strValue(); BsonDocument bsonQueryDocument = BsonDocument.parse(request.getFirstChild("filter").strValue()); prepareBsonQueryData(bsonQueryDocument, request.getFirstChild("filter")); printlnJson("Update filter", bsonQueryDocument); BsonDocument bsonDocument = BsonDocument.parse(request.getFirstChild("documentUpdate").strValue()); printlnJson("Update documentUpdate", bsonDocument); prepareBsonQueryData(bsonDocument, request.getFirstChild("documentUpdate")); printlnJson("Update documentUpdate", bsonDocument); showLog(); if (request.hasChildren("writeConcern")) { WriteConcern writeConcern = new WriteConcern(); if (request.getFirstChild("writeConcern").hasChildren("journal")) { writeConcern.withJournal( request.getFirstChild("writeConcern").getFirstChild("journal").boolValue()); } if (request.getFirstChild("writeConcern").hasChildren("w")) { if (request.getFirstChild("writeConcern").getFirstChild("w").isInt()) { writeConcern.withW(request.getFirstChild("writeConcern").getFirstChild("w").intValue()); } if (request.getFirstChild("writeConcern").getFirstChild("w").isString()) { writeConcern.withW(request.getFirstChild("writeConcern").getFirstChild("w").strValue()); } } if (request.getFirstChild("writeConcern").hasChildren("timeout")) { writeConcern.withWTimeout( request.getFirstChild("writeConcern").getFirstChild("timeout").longValue(), TimeUnit.MILLISECONDS); } db.getCollection(collectionName, BsonDocument.class).withWriteConcern(writeConcern); } if (request.hasChildren("options")) { UpdateOptions updateOptions = new UpdateOptions(); updateOptions.upsert(request.getFirstChild("options").getFirstChild("upsert").boolValue()); updateOptions.bypassDocumentValidation( request.getFirstChild("options").getFirstChild("bypassDocumentValidation").boolValue()); UpdateResult resultUpdate = db.getCollection(collectionName, BsonDocument.class) .updateOne(bsonQueryDocument, bsonDocument, updateOptions); v.getNewChild("matchedCount").assignValue(Value.create(resultUpdate.getMatchedCount())); v.getNewChild("modifiedCount").assignValue(Value.create(resultUpdate.getModifiedCount())); } else { UpdateResult resultUpdate = db.getCollection(collectionName, BsonDocument.class) .updateOne(bsonQueryDocument, bsonDocument); v.getNewChild("matchedCount").assignValue(Value.create(resultUpdate.getMatchedCount())); v.getNewChild("modifiedCount").assignValue(Value.create(resultUpdate.getModifiedCount())); } return v; } catch (MongoException ex) { throw new FaultException("MongoException", ex); } }
From source file:MyLibrary.DoMongodb.java
/** * mongodb//from ww w .ja va 2 s . co m * @param MongoDatabase1 MongoDatabase * @param collections collections * @param JsonObject1 * @param JsonObject2 * @param isUpsert * @return null */ public String update(MongoDatabase MongoDatabase1, String collections, JSONObject JsonObject1, JSONObject JsonObject2, boolean isUpsert) { String result = null; Document Document1 = new Document(); Document Document2 = new Document(); try { Document1 = JsonToDocument(JsonObject1); Document2 = JsonToDocument(JsonObject2); UpdateOptions UpdateOptions1 = new UpdateOptions(); UpdateOptions1.upsert(isUpsert); MongoDatabase1.getCollection(collections).updateOne(Document1, new Document("$set", Document2), UpdateOptions1); } catch (Exception e) { result = e.getMessage(); } return result; }
From source file:net.es.netshell.mongodb.MongoDBProvider.java
License:Open Source License
@Override public final void store(String user, String collection, PersistentObject obj) throws IOException { String collectionName = user + "_" + collection; if (!KernelThread.currentKernelThread().isPrivileged()) { throw new SecurityException("store db one - not authorized"); }//w w w .ja v a 2 s . c o m MongoCollection mongoCollection = this.db.getCollection(collectionName); if (mongoCollection == null) { throw new RuntimeErrorException(new Error("Could not store into collection " + collectionName)); } Document doc = Document.parse(obj.saveToJSON()); Document query = new Document("resourceName", obj.getResourceName()); UpdateOptions options = new UpdateOptions(); options.upsert(true); mongoCollection.replaceOne(query, doc, options); }
From source file:org.apache.eagle.alert.metadata.impl.MongoMetadataDaoImpl.java
License:Apache License
private <T> OpResult addOrReplace(MongoCollection<Document> collection, T t) { BsonDocument filter = new BsonDocument(); if (t instanceof StreamDefinition) { filter.append("streamId", new BsonString(MetadataUtils.getKey(t))); } else if (t instanceof PublishmentType) { filter.append("type", new BsonString(MetadataUtils.getKey(t))); } else if (t instanceof AlertPublishEvent) { filter.append("alertId", new BsonString(MetadataUtils.getKey(t))); } else {/*from w ww .j a va 2 s.c om*/ filter.append("name", new BsonString(MetadataUtils.getKey(t))); } String json = ""; OpResult result = new OpResult(); try { json = mapper.writeValueAsString(t); UpdateOptions options = new UpdateOptions(); options.upsert(true); UpdateResult ur = collection.replaceOne(filter, Document.parse(json), options); // FIXME: could based on matched count do better matching... if (ur.getModifiedCount() > 0 || ur.getUpsertedId() != null) { result.code = 200; result.message = String.format("update %d configuration item.", ur.getModifiedCount()); } else { result.code = 500; result.message = "no configuration item create/updated."; } } catch (Exception e) { result.code = 500; result.message = e.getMessage(); LOG.error("", e); } return result; }
From source file:org.apache.storm.mongodb.common.MongoDBClient.java
License:Apache License
/** * Update all documents in the collection according to the specified query filter. * When upsert set to true, the new document will be inserted if there are no matches to the query filter. * /*from ww w .j av a2s . c o m*/ * @param filter * @param update * @param upsert */ public void update(Bson filter, Bson update, boolean upsert) { UpdateOptions options = new UpdateOptions(); if (upsert) { options.upsert(true); } collection.updateMany(filter, update, options); }
From source file:soatreefinder.SOATreeFinder.java
public static void loadMongoFromSQL(String sqlQuery, String mongoDBName, String mongoCollName, String mongoCollPrimaryKey) throws SQLException, IOException, FileNotFoundException { propsFile.load(new FileInputStream(System.getProperty("user.dir") + "\\context.properties")); Statement stmt = null;/* w w w .j a v a 2 s . co m*/ String blobString; Map sqlMap = new HashMap(); MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress( propsFile.getProperty("mongoHost"), Integer.parseInt(propsFile.getProperty("mongoPort"))))); MongoDatabase db = mongoClient.getDatabase(mongoDBName); MongoCollection coll = db.getCollection(mongoCollName); UpdateOptions upsertOption = new UpdateOptions(); upsertOption.upsert(true); Connection soadb_Connection = DriverManager .getConnection("jdbc:oracle:thin:" + propsFile.getProperty("user") + "/" + propsFile.getProperty("password") + "@//" + propsFile.getProperty("host") + ":" + propsFile.getProperty("port") + "/" + propsFile.getProperty("service")); stmt = soadb_Connection.createStatement(); ResultSet rs = stmt.executeQuery(sqlQuery); ResultSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { for (int i = 1; i <= rsmd.getColumnCount(); i++) { /* The following block of switch statement checks for the sql data type of a given column and puts the appropriate object into the hash map*/ switch (rsmd.getColumnType(i)) { case 12: sqlMap.put(rsmd.getColumnName(i), rs.getString(i)); break; case -9: sqlMap.put(rsmd.getColumnName(i), rs.getString(i)); break; case -15: sqlMap.put(rsmd.getColumnName(i), rs.getString(i)); break; case 16: sqlMap.put(rsmd.getColumnName(i), rs.getBoolean(i)); break; case -7: sqlMap.put(rsmd.getColumnName(i), rs.getByte(i)); break; case 2: sqlMap.put(rsmd.getColumnName(i), rs.getInt(i)); break; case 4: sqlMap.put(rsmd.getColumnName(i), rs.getInt(i)); break; case 6: sqlMap.put(rsmd.getColumnName(i), rs.getFloat(i)); break; case 8: sqlMap.put(rsmd.getColumnName(i), rs.getDouble(i)); break; case 3: sqlMap.put(rsmd.getColumnName(i), rs.getBigDecimal(i)); break; case 1: sqlMap.put(rsmd.getColumnName(i), rs.getString(i)); break; case 91: sqlMap.put(rsmd.getColumnName(i), rs.getDate(i).toString()); break; case 92: sqlMap.put(rsmd.getColumnName(i), rs.getTime(i).toString()); break; case 93: sqlMap.put(rsmd.getColumnName(i), rs.getTimestamp(i).toString()); break; case 2003: sqlMap.put(rsmd.getColumnName(i), rs.getArray(i)); break; case 2004: { Blob blob = rs.getBlob(i); byte[] bdata = blob.getBytes(1, (int) blob.length()); blobString = new String(bdata); sqlMap.put(rsmd.getColumnName(i), blobString); } break; case 2005: sqlMap.put(rsmd.getColumnName(i), rs.getClob(i)); break; default: sqlMap.put(rsmd.getColumnName(i), rs.getString(i)); } } // Upsert sql record into mongo collection coll.updateOne(eq(mongoCollPrimaryKey, sqlMap.get(mongoCollPrimaryKey)), new Document("$set", new Document(sqlMap)), upsertOption); } }