Java Code Examples for com.mongodb.client.MongoCollection#findOneAndReplace()

The following examples show how to use com.mongodb.client.MongoCollection#findOneAndReplace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoEventStorage.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void update(final Event old, final Event updated) throws StaleUpdateException, EventStorageException {
    requireNonNull(old);
    requireNonNull(updated);

    // The updated entity must have the same Subject as the one it is replacing.
    if(!old.getSubject().equals(updated.getSubject())) {
        throw new EventStorageException("The old Event and the updated Event must have the same Subject. " +
                "Old Subject: " + old.getSubject().getData() + ", Updated Subject: " + updated.getSubject().getData());
    }

    final Set<Bson> filters = new HashSet<>();

    // Must match the old entity's Subject.
    filters.add( makeSubjectFilter(old.getSubject()) );

    // Do a find and replace.
    final Bson oldEntityFilter = Filters.and(filters);
    final Document updatedDoc = EVENT_CONVERTER.toDocument(updated);

    final MongoCollection<Document> collection = mongo.getDatabase(ryaInstanceName).getCollection(COLLECTION_NAME);
    if(collection.findOneAndReplace(oldEntityFilter, updatedDoc) == null) {
        throw new StaleUpdateException("Could not update the Event with Subject '" + updated.getSubject().getData() + ".");
    }
}
 
Example 2
Source File: MongoSubscriptionTask.java    From epcis with Apache License 2.0 6 votes vote down vote up
private void updateInitialRecordTime(String subscriptionID, String initialRecordTime) {

		MongoCollection<BsonDocument> collection = Configuration.mongoDatabase.getCollection("Subscription",
				BsonDocument.class);

		BsonDocument subscription = collection.find(new BsonDocument("subscriptionID", new BsonString(subscriptionID)))
				.first();
		subscription.put("initialRecordTime", new BsonString(initialRecordTime));

		if (subscription != null) {
			collection.findOneAndReplace(new BsonDocument("subscriptionID", new BsonString(subscriptionID)),
					subscription);
		}
		Configuration.logger.log(Level.INFO,
				"InitialRecordTime of Subscription ID: " + subscriptionID + " is updated to DB. ");
	}
 
Example 3
Source File: MongoCaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private boolean replaceErroneousEvents(MongoCollection<BsonDocument> collection, BsonDocument filter,
		BsonDocument object2Save) {
	boolean isReplacing = false;
	while (true) {
		Object result = collection.findOneAndReplace(filter, object2Save);

		if (result == null)
			break;
		else {
			isReplacing = true;

		}
	}
	return isReplacing;
}
 
Example 4
Source File: MongoSubscriptionTask.java    From epcis with Apache License 2.0 6 votes vote down vote up
private void updateInitialRecordTime(String subscriptionID, String initialRecordTime) {

		MongoCollection<BsonDocument> collection = Configuration.mongoDatabase.getCollection("Subscription",
				BsonDocument.class);

		BsonDocument subscription = collection.find(new BsonDocument("subscriptionID", new BsonString(subscriptionID)))
				.first();
		subscription.put("initialRecordTime", new BsonString(initialRecordTime));

		if (subscription != null) {
			collection.findOneAndReplace(new BsonDocument("subscriptionID", new BsonString(subscriptionID)),
					subscription);
		}
		Configuration.logger.log(Level.INFO,
				"InitialRecordTime of Subscription ID: " + subscriptionID + " is updated to DB. ");
	}
 
Example 5
Source File: MongoCaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private boolean replaceErroneousEvents(MongoCollection<BsonDocument> collection, BsonDocument filter,
		BsonDocument object2Save) {
	boolean isReplacing = false;
	while (true) {
		Object result = collection.findOneAndReplace(filter, object2Save);

		if (result == null)
			break;
		else {
			isReplacing = true;

		}
	}
	return isReplacing;
}
 
Example 6
Source File: MongoDocumentDb.java    From mdw with Apache License 2.0 5 votes vote down vote up
@Override
public boolean updateDocument(String ownerType, Long documentId, String content) {
    MongoCollection<org.bson.Document> collection = null;
    MongoDatabase mongoDb =  getMongoDb();
    if (mongoDb != null)
        collection = mongoDb.getCollection(getCollectionName(ownerType));
    org.bson.Document myDoc = null;
    if (content != null && content.startsWith("{")) {
        try {
            // Parse JSON to create BSON CONTENT Document
            org.bson.Document myJsonDoc = org.bson.Document.parse(content);
            if (!myJsonDoc.isEmpty()) {
                if (content.contains(".") || content.contains("$"))
                    myJsonDoc = encodeMongoDoc(myJsonDoc);
                // Plus append isJSON:true field
                myDoc = new org.bson.Document("CONTENT", myJsonDoc).append("document_id", documentId).append("isJSON", true);
            }
        }
        catch (Throwable ex) {myDoc=null;}  // Assume not JSON then
    }
    if (myDoc == null)   // Create BSON document with Raw content if it wasn't JSON plus append isJSON:false
        myDoc = new org.bson.Document("CONTENT", content).append("document_id", documentId).append("isJSON", false);
    if (collection != null && collection.findOneAndReplace(eq("document_id", documentId), myDoc) != null)
        return true;
    else {
        // Must have been null content initially and being updated now to non-null
        if (collection != null)
            collection.insertOne(myDoc);
        return true;
    }
}
 
Example 7
Source File: MongoEntityStorage.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void update(final Entity old, final Entity updated) throws StaleUpdateException, EntityStorageException {
    requireNonNull(old);
    requireNonNull(updated);

    // The updated entity must have the same Subject as the one it is replacing.
    if(!old.getSubject().equals(updated.getSubject())) {
        throw new EntityStorageException("The old Entity and the updated Entity must have the same Subject. " +
                "Old Subject: " + old.getSubject().getData() + ", Updated Subject: " + updated.getSubject().getData());
    }

    // Make sure the updated Entity has a higher verison.
    if(old.getVersion() >= updated.getVersion()) {
        throw new EntityStorageException("The old Entity's version must be less than the updated Entity's version." +
                " Old version: " + old.getVersion() + " Updated version: " + updated.getVersion());
    }

    final Set<Bson> filters = new HashSet<>();

    // Must match the old entity's Subject.
    filters.add( makeSubjectFilter(old.getSubject()) );

    // Must match the old entity's Version.
    filters.add( makeVersionFilter(old.getVersion()) );

    // Do a find and replace.
    final Bson oldEntityFilter = Filters.and(filters);
    final Document updatedDoc = ENTITY_CONVERTER.toDocument(updated);

    final MongoCollection<Document> collection = mongo.getDatabase(ryaInstanceName).getCollection(COLLECTION_NAME);
    if(collection.findOneAndReplace(oldEntityFilter, updatedDoc) == null) {
        throw new StaleUpdateException("Could not update the Entity with Subject '" + updated.getSubject().getData() + ".");
    }
}