@Override public Integer call() { collection().replaceOne(convertToBson(criteria), document, new UpdateOptions().upsert(true)); // upsert will always return 1: // if document doesn't exists, it will be inserted (modCount == 1) // if document exists, it will be updated (modCount == 1) return 1; } });
private void removeConfigPath() { final FindIterable<Document> documentsWithConfigPath = collection.find(exists("configuration_path")); for (Document document : documentsWithConfigPath) { final ObjectId objectId = document.getObjectId("_id"); document.remove("configuration_path"); final UpdateResult updateResult = collection.replaceOne(eq("_id", objectId), document); if (updateResult.wasAcknowledged()) { LOG.debug("Successfully updated document with ID <{}>", objectId); } else { LOG.error("Failed to update document with ID <{}>", objectId); } } }
collection.replaceOne(new Document("_id", toInsert.get("_id")), toInsert, UPDATE_WITH_UPSERT); } else {
@Override public void upgrade() { final FindIterable<Document> documentsWithMissingFields = collection.find(or(not(exists(ContentPack.FIELD_META_ID)), not(exists(ContentPack.FIELD_META_REVISION)))); for (Document document : documentsWithMissingFields) { final ObjectId objectId = document.getObjectId("_id"); LOG.debug("Found document with missing \"id\" or \"rev\" field with ID <{}>", objectId); final String id = document.get("id", objectId.toHexString()); final int rev = document.get("rev", 0); document.put("id", id); document.put("rev", rev); final UpdateResult updateResult = collection.replaceOne(eq("_id", objectId), document); if (updateResult.wasAcknowledged()) { LOG.debug("Successfully updated document with ID <{}>", objectId); } else { LOG.error("Failed to update document with ID <{}>", objectId); } } } }
public Object doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { if (dbId != null) { collection.replaceOne(Filters.eq("_id", dbId.get("_id")), dbDoc); } else { if (dbDoc.containsKey("_id") && dbDoc.get("_id") == null) { dbDoc.remove("_id"); } collection.insertOne(dbDoc); } return null; } });
public Object doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass, dbDoc, null); WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction); MappedDocument mapped = MappedDocument.of(dbDoc); if (!mapped.hasId()) { if (writeConcernToUse == null) { collection.insertOne(dbDoc); } else { collection.withWriteConcern(writeConcernToUse).insertOne(dbDoc); } } else if (writeConcernToUse == null) { collection.replaceOne(mapped.getIdFilter(), dbDoc, new ReplaceOptions().upsert(true)); } else { collection.withWriteConcern(writeConcernToUse).replaceOne(mapped.getIdFilter(), dbDoc, new ReplaceOptions().upsert(true)); } return mapped.getId(); } });
replaceOptions.upsert(opts.isUpsert()); return collection.replaceOne(queryObj, updateObj, replaceOptions); } else { if (multi) {
collection.replaceOne(query, (Document)doc, new UpdateOptions().upsert(upsert)); } else { BasicDBObject update = (BasicDBObject)doc;
public Object doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass, dbDoc, null); WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction); MappedDocument mapped = MappedDocument.of(dbDoc); if (!mapped.hasId()) { if (writeConcernToUse == null) { collection.insertOne(dbDoc); } else { collection.withWriteConcern(writeConcernToUse).insertOne(dbDoc); } } else if (writeConcernToUse == null) { collection.replaceOne(mapped.getIdFilter(), dbDoc, new ReplaceOptions().upsert(true)); } else { collection.withWriteConcern(writeConcernToUse).replaceOne(mapped.getIdFilter(), dbDoc, new ReplaceOptions().upsert(true)); } return mapped.getId(); } });
replaceOptions.upsert(opts.isUpsert()); return collection.replaceOne(queryObj, updateObj, replaceOptions); } else { if (multi) {
@Test public void testUpdate() throws Exception { MongoCollection<Document> collection = readOnlyClient.getDatabase("testdb").getCollection("testcollection"); Document object = new Document("_id", 1); Document newObject = new Document("_id", 1); assertThatExceptionOfType(MongoException.class) .isThrownBy(() -> collection.replaceOne(object, newObject)) .withMessageContaining("Command failed with error 59 (CommandNotFound): 'no such command: 'update'"); }
public void saveTag(@Nonnull Tag tag) { writeLock.lock(); try { checkNotNull(tag); Document document = toDocument(tag); getCollection().replaceOne(toFilter(tag), document, upsert()); } finally { writeLock.unlock(); } }
public void save(@Nonnull ProjectEntityCrudKitSettings settings) { collection.replaceOne(withProjectId(settings.getProjectId()), converter.toDocument(checkNotNull(settings)), new UpdateOptions().upsert(true)); } }
@Override public void update(DeployExecution deployExecution) { MongoDatabase database = mongoClient.getDatabase(env.getPhysicalSchema(deployExecution.getSchema()).getPhysicalName()); MongoCollection<Document> auditCollection = database.getCollection(deployExecutionTableName); auditCollection.replaceOne(getChangeFilter(deployExecution), getDocumentFromDeployExecution(deployExecution, true)); }
@NotNull @Override @Deprecated public UpdateResult replaceOne(@NotNull Bson filter, @NotNull TDocument replacement, @NotNull UpdateOptions updateOptions) { maybeThrowExceptionBeforeUpdate(); UpdateResult result = collection.replaceOne(filter, replacement, updateOptions); maybeThrowExceptionAfterUpdate(); return result; }
@NotNull @Override public UpdateResult replaceOne(@NotNull Bson filter, @NotNull TDocument replacement, @NotNull ReplaceOptions replaceOptions) { maybeThrowExceptionBeforeUpdate(); UpdateResult result = collection.replaceOne(filter, replacement, replaceOptions); maybeThrowExceptionAfterUpdate(); return result; }
@NotNull @Override public UpdateResult replaceOne(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull TDocument replacement) { maybeThrowExceptionBeforeUpdate(); UpdateResult result = collection.replaceOne(clientSession, filter, replacement); maybeThrowExceptionAfterUpdate(); return result; }
@NotNull @Override public UpdateResult replaceOne(@NotNull Bson filter, @NotNull TDocument replacement) { maybeThrowExceptionBeforeUpdate(); UpdateResult result = collection.replaceOne(filter, replacement); maybeThrowExceptionAfterUpdate(); return result; }
public void save(@Nonnull ProjectDetails projectRecord) { Document document = objectMapper.convertValue(projectRecord, Document.class); collection.replaceOne(withProjectId(projectRecord.getProjectId()), document, new UpdateOptions().upsert(true)); cache.invalidate(projectRecord.getProjectId()); displayLanguagesCache.invalidate(projectRecord.getProjectId()); }
@Test public void testUpdateIdNoChange() { collection.insertOne(json("_id: 1")); collection.replaceOne(json("_id: 1"), json("_id: 1, a: 5")); assertThat(collection.find(json("_id: 1")).first()).isEqualTo(json("_id: 1, a: 5")); collection.updateOne(json("_id: 1"), json("$set: {_id: 1, b: 3}")); assertThat(collection.find(json("_id: 1")).first()).isEqualTo(json("_id: 1, a: 5, b: 3")); // test with $set collection.updateOne(json("_id: 1"), json("$set: {_id: 1, a: 7}")); assertThat(collection.find(json("_id: 1")).first()).isEqualTo(json("_id: 1, a: 7, b: 3")); }