@Test public void testUpdateNothing() throws Exception { Document object = json("_id: 1"); UpdateResult result = collection.replaceOne(object, object); assertThat(result.getModifiedCount()).isEqualTo(0); assertThat(result.getMatchedCount()).isEqualTo(0); assertThat(result.getUpsertedId()).isNull(); }
@Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { final Document query = new Document(); query.put("_id", item.getId()); final Document update = new Document(); final Document set = new Document(); set.put("checked", b); update.put("$set", set); _itemState.put(item.getId(), b); UpdateResult result = _itemSource.updateOne(query, update); _itemState.remove(result.getUpsertedId()); } });
@Test public void testUpsertWithoutId() { UpdateResult result = collection.updateOne(eq("a", 1), set("a", 2), new UpdateOptions().upsert(true)); assertThat(result.getModifiedCount()).isEqualTo(0); assertThat(result.getUpsertedId()).isNotNull(); assertThat(collection.find().first().get("_id")).isInstanceOf(ObjectId.class); assertThat(collection.find().first().get("a")).isEqualTo(2); }
@Test public void testUpdate() throws Exception { Document object = json("_id: 1"); Document newObject = json("_id: 1, foo: 'bar'"); collection.insertOne(object); UpdateResult result = collection.replaceOne(object, newObject); assertThat(result.getModifiedCount()).isEqualTo(1); assertThat(result.getUpsertedId()).isNull(); assertThat(collection.find(object).first()).isEqualTo(newObject); }
@Test public void testUpsert() { UpdateResult result = collection.updateMany(json("n:'jon'"), json("$inc: {a: 1}"), new UpdateOptions().upsert(true)); assertThat(result.getModifiedCount()).isEqualTo(0); Document object = collection.find().first(); assertThat(result.getUpsertedId()).isEqualTo(new BsonObjectId(object.getObjectId("_id"))); object.remove("_id"); assertThat(object).isEqualTo(json("n: 'jon', a: 1")); result = collection.updateOne(json("_id: 17, n: 'jon'"), json("$inc: {a: 1}"), new UpdateOptions().upsert(true)); assertThat(result.getUpsertedId()).isEqualTo(new BsonInt32(17)); assertThat(collection.find(json("_id: 17")).first()).isEqualTo(json("_id: 17, n: 'jon', a: 1")); }
@Override protected void storeBlock(byte[] digest, int level, byte[] data) throws IOException { String id = StringUtils.convertBytesToHex(digest); cache.put(id, data); // Create the mongo blob object BasicDBObject mongoBlob = new BasicDBObject(MongoBlob.KEY_ID, id); mongoBlob.append(MongoBlob.KEY_DATA, data); mongoBlob.append(MongoBlob.KEY_LEVEL, level); // If update only the lastMod needs to be modified BasicDBObject updateBlob =new BasicDBObject(MongoBlob.KEY_LAST_MOD, System.currentTimeMillis()); BasicDBObject upsert = new BasicDBObject(); upsert.append("$setOnInsert", mongoBlob) .append("$set", updateBlob); try { Bson query = getBlobQuery(id, -1); UpdateOptions options = new UpdateOptions().upsert(true); UpdateResult result = getBlobCollection().updateOne(query, upsert, options); if (result != null && result.getUpsertedId() == null) { LOG.trace("Block with id [{}] updated", id); } else { LOG.trace("Block with id [{}] created", id); } } catch (MongoException e) { throw new IOException(e.getMessage(), e); } }
@Override protected void storeBlock(byte[] digest, int level, byte[] data) throws IOException { String id = StringUtils.convertBytesToHex(digest); cache.put(id, data); // Create the mongo blob object BasicDBObject mongoBlob = new BasicDBObject(MongoBlob.KEY_ID, id); mongoBlob.append(MongoBlob.KEY_DATA, data); mongoBlob.append(MongoBlob.KEY_LEVEL, level); // If update only the lastMod needs to be modified BasicDBObject updateBlob =new BasicDBObject(MongoBlob.KEY_LAST_MOD, System.currentTimeMillis()); BasicDBObject upsert = new BasicDBObject(); upsert.append("$setOnInsert", mongoBlob) .append("$set", updateBlob); try { Bson query = getBlobQuery(id, -1); UpdateOptions options = new UpdateOptions().upsert(true); UpdateResult result = getBlobCollection().updateOne(query, upsert, options); if (result != null && result.getUpsertedId() == null) { LOG.trace("Block with id [{}] updated", id); } else { LOG.trace("Block with id [{}] created", id); } } catch (MongoException e) { throw new IOException(e.getMessage(), e); } }
UpdateResult ur = collection.replaceOne(filter, Document.parse(json), options); if (ur.getModifiedCount() > 0 || ur.getUpsertedId() != null) { result.code = 200; result.message = String.format("update %d configuration item.", ur.getModifiedCount());
/** * {@inheritDoc} */ @Override protected final Source<Boolean, NotUsed> save(final Thing thing, final long revision, final long policyRevision) { log.debug("Saving Thing with revision <{}> and policy revision <{}>: <{}>", revision, policyRevision, thing); final Bson filter = filterWithLowerThingRevisionOrLowerPolicyRevision(getThingId(thing), revision, policyRevision); final Document document = toUpdate(ThingDocumentMapper.toDocument(thing), revision, policyRevision); return Source.fromPublisher(collection.updateOne(filter, document, new UpdateOptions().upsert(true))) .map(updateResult -> updateResult.getMatchedCount() > 0 || null != updateResult.getUpsertedId()); }
/** * {@inheritDoc} */ @Override protected final Source<Boolean, NotUsed> save(final Thing thing, final long revision, final long policyRevision) { log.debug("Saving Thing with revision <{}> and policy revision <{}>: <{}>", revision, policyRevision, thing); final Bson filter = filterWithLowerThingRevisionOrLowerPolicyRevision(getThingId(thing), revision, policyRevision); final Document document = toUpdate(ThingDocumentMapper.toDocument(thing), revision, policyRevision); return Source.fromPublisher(collection.updateOne(filter, document, new UpdateOptions().upsert(true))) .map(updateResult -> updateResult.getMatchedCount() > 0 || null != updateResult.getUpsertedId()); }
private void insertUpdate(LockEntry newLock, boolean onlyIfSameOwner) { boolean lockHeld; try { final Bson acquireLockQuery = getAcquireLockQuery(newLock.getKey(), newLock.getOwner(), onlyIfSameOwner); final UpdateResult result = collection.updateMany( acquireLockQuery, new Document().append("$set", newLock.buildFullDBObject()), new UpdateOptions().upsert(!onlyIfSameOwner)); lockHeld = result.getModifiedCount() <= 0 && result.getUpsertedId() == null; } catch (MongoWriteException ex) { lockHeld = ex.getError().getCategory() == ErrorCategory.DUPLICATE_KEY; if (!lockHeld) { throw ex; } } catch (DuplicateKeyException ex) { lockHeld = true; } if (lockHeld) { throw new LockPersistenceException("Lock is held"); } }
.thenApply( result -> AsyncPropertyBoxOperationResultContext.create(context, collection, configuration, (result.getUpsertedId() != null) ? 1 : MongoOperations.getAffectedCount(result), (result.getUpsertedId() != null) ? OperationType.INSERT : OperationType.UPDATE, configuration.getValue(), document, result.getUpsertedId()));
final BsonValue upsertedId = result.getUpsertedId();