This watcher that I coded (using Android app coded with java ) will detect an update using MongoDB compass but not when I run the same update from a function using a http endpoint from postman.
I don’t know why. Any help would be appreciated, Thanks.
RealmEventStreamAsyncTask<RealmLogEntry> watcher3 = mongoCollection
.watchWithFilterAsync(new Document("fullDocument.userId", "18"));
watcher3.get(result3 -> {
try {
if (result3.isSuccess()) {
Log.v("EXAMPLE", "Event type watcher 3: " +
result3.get().getOperationType() + " full document: " +
result3.get().getFullDocument());
} else {
Log.e("EXAMPLE",
"failed to subscribe to filtered changes in the collection with : ",
result3.getError());
}
} catch (NullPointerException npe) {
Log.d(TAG, "onCreate: potential delete by frig ???????");
}
});
the function that I am running, that is not being detected by the watcher is :
return collection.findOneAndReplace(query, replacement, options)
.then(replacedDocument => {
if(replacedDocument) {
console.log(`Successfully replaced the following document: ${replacedDocument}.`);
response.setStatusCode(200); // Set an HTTP Status code like "201 - created"
response.setBody(JSON.stringify({ "code": 200, "message": "update ok", "timestamp": dateBob }));
} else {
console.log("No document matches the provided query.")
response.setStatusCode(400); // Set an HTTP Status code like "201 - created"
response.setBody(JSON.stringify({ "code": 400, "message": "update error A", "timestamp": dateBob }));
}
// return updatedDocument
})
.catch(err => {
console.error(`Failed to find and replace document: ${err}`)
response.setStatusCode(400); // Set an HTTP Status code like "201 - created"
response.setBody(JSON.stringify({ "code": 400, "message": "update error B", "timestamp": dateBob }));
}
)
The difference is likely that using Data Explorer synthesizes “Replace” events but findOneAndUpdate() synthesizes an “Update” event which will not have the FullDocument field unless you ask for it