The following code illustrates the issue.
// This works as expected
// Creates: {"_id": "as_expected", "value": 100}
collection
.findOneAndUpdate(
BsonDocument.parse("{\"_id\": \"as_expected\"}"),
BsonDocument.parse("{\"$set\": {\"value\": 100}}"),
new FindOneAndUpdateOptions().upsert(true));
// This generates an identifier instead of using the one in the $and-list
// Creates: {"_id": {"$oid": "f6a40f2c1855632c12f51c59"}, "value": 200}
collection
.findOneAndUpdate(
BsonDocument.parse("{\"$and\": [{\"_id\": \"this_goes_gone\"}]}"),
BsonDocument.parse("{\"$set\": {\"value\": 200}}"),
new FindOneAndUpdateOptions().upsert(true));
The same request towards the official MonoDB would find and use the _id supplied in the query/filter.
The following code illustrates the issue.
The same request towards the official MonoDB would find and use the _id supplied in the query/filter.