List of usage examples for com.mongodb.client MongoCollection replaceOne
UpdateResult replaceOne(ClientSession clientSession, Bson filter, TDocument replacement);
From source file:cn.edu.hfut.dmic.webcollector.plugin.mongo.MongoDBUtils.java
License:Open Source License
public static void updateOrInsert(MongoCollection col, Document doc) { String key = doc.getString("_id"); Document idDoc = new Document("_id", key); col.replaceOne(idDoc, doc, forcedUo); }
From source file:cn.edu.hfut.dmic.webcollector.plugin.mongo.MongoDBUtils.java
License:Open Source License
public static void updateOrInsert(MongoCollection col, CrawlDatum datum) { Document doc = CrawlDatumFormater.datumToBson(datum); String key = datum.getKey();//www .j ava2s . c o m Document idDoc = new Document("_id", key); col.replaceOne(idDoc, doc, forcedUo); }
From source file:com.my.food.recipe.db.MongoDb.java
public void putRecipeSummary(RecipeSummary recipeSummary) { MongoCollection<BsonDocument> collectionRecipeSummary = database.getCollection("recipeSummary", BsonDocument.class); BsonDocument bsonDocument = BsonDocument.parse(recipeSummary.toJson()); bsonDocument.put("_id", new BsonString(recipeSummary.getRecipeId())); LOG.info("put bsonDocument = {}", bsonDocument.toString()); collectionRecipeSummary.replaceOne(eq("_id", recipeSummary.getRecipeId()), bsonDocument, new UpdateOptions().upsert(true)); // collectionRecipeSummary.insertOne(bsonDocument); }
From source file:com.yahoo.ycsb.db3.MongoDbClient.java
License:Open Source License
/** * Insert a record in the database. Any field/value pairs in the specified * values HashMap will be written into the record with the specified record * key./* w ww . j a va 2 s . c o m*/ * * @param table * The name of the table * @param key * The record key of the record to insert. * @param values * A HashMap of field/value pairs to insert in the record * @return Zero on success, a non-zero error code on error. See the {@link DB} * class's description for a discussion of error codes. */ @Override public Status insert(String table, String key, HashMap<String, ByteIterator> values) { try { MongoCollection<Document> collection = database.getCollection(table); Document toInsert = new Document("_id", key); for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { toInsert.put(entry.getKey(), entry.getValue().toArray()); } if (batchSize == 1) { if (useUpsert) { // this is effectively an insert, but using an upsert instead due // to current inability of the framework to clean up after itself // between test runs. collection.replaceOne(new Document("_id", toInsert.get("_id")), toInsert, UPDATE_WITH_UPSERT); } else { collection.insertOne(toInsert); } } else { bulkInserts.add(toInsert); if (bulkInserts.size() == batchSize) { if (useUpsert) { List<UpdateOneModel<Document>> updates = new ArrayList<UpdateOneModel<Document>>( bulkInserts.size()); for (Document doc : bulkInserts) { updates.add(new UpdateOneModel<Document>(new Document("_id", doc.get("_id")), doc, UPDATE_WITH_UPSERT)); } collection.bulkWrite(updates); } else { collection.insertMany(bulkInserts, INSERT_UNORDERED); } bulkInserts.clear(); } else { return OptionsSupport.BATCHED_OK; } } return Status.OK; } catch (Exception e) { System.err.println("Exception while trying bulk insert with " + bulkInserts.size()); e.printStackTrace(); return Status.ERROR; } }
From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java
License:MIT License
@Override public boolean saveUser(User user) { user.getIoLock().lock();//from w w w . j a v a2 s. c o m try { MongoCollection<Document> c = database.getCollection(prefix + "users"); if (!GenericUserManager.shouldSave(user)) { return c.deleteOne(new Document("_id", user.getUuid())).wasAcknowledged(); } else { return c.replaceOne(new Document("_id", user.getUuid()), userToDoc(user), new UpdateOptions().upsert(true)).wasAcknowledged(); } } finally { user.getIoLock().unlock(); } }
From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java
License:MIT License
@Override public boolean saveGroup(Group group) { group.getIoLock().lock();/*www . j av a 2 s .co m*/ try { MongoCollection<Document> c = database.getCollection(prefix + "groups"); return c.replaceOne(new Document("_id", group.getName()), groupToDoc(group), new UpdateOptions().upsert(true)).wasAcknowledged(); } finally { group.getIoLock().unlock(); } }
From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java
License:MIT License
@Override public boolean saveUUIDData(UUID uuid, String username) { MongoCollection<Document> c = database.getCollection(prefix + "uuid"); c.replaceOne(new Document("_id", uuid), new Document("_id", uuid).append("name", username.toLowerCase()), new UpdateOptions().upsert(true)); return true;//w w w .j a va2s.co m }
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 . j a va 2s. co 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:net.liaocy.ml4j.db.Mongo.java
public static void save(MongoCollection<Document> collection, Document document) { Object id = document.get("_id"); if (id == null) { collection.insertOne(document);//from w w w .j a va 2 s .co m } else { collection.replaceOne(eq("_id", id), document, new UpdateOptions().upsert(true)); } }
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 w w .ja v a 2 s. c o m 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; }