I am working on this legacy application (7 years old). I have many methods that do the same thing that I am trying to upgrade to a newer MongoDB Java driver, but it won’t compile.
@Override
public void saveOrUpdatePrinter(Document printer) {
printer.put(PRINTER_COLUMNS.updateDate,new Date());
MongoCollection<Document> collection = mongoTemplate.getCollection("PRINTERS");
printer.remove("_id");
Document query = new Document().append(PRINTER_COLUMNS.internal_id, printer.get(PRINTER_COLUMNS.internal_id));
WriteResult result = collection.update(query, printer, true, false);
logger.debug("saveOrUpdatePrinter updeded records: " + result.getN());
}//
The error is:
The method update(Document, Document, boolean, boolean) is undefined
for the type MongoCollection<Document>
Why was this removed?
printer.remove("_id");
Also I would like to know how to do either update or save on the document in one go?
And what will be the proper way to update a single document in the new (MongoDB Java driver 4.7.0)?
Reading this code a little more seems like it was an attempt to do UPSERT operation (update or insert).
2
Answers
You can update a document using the MongoCollection#updateOne() method
An example would be:
I will try to answer your questions.
Q : How to do either Update or Save on the Document in one go?
-> MongoDB’s
update
method updates the values in the existing document whereas thesave
method replaces the existing document with the document passed. Nothing happens in one go.update
method only updates which are specific fields which are modified by comparing the fields from the modified document with the original document whereas thesave
method updates/replaces the values of all the fields of an original document by the taking values from the modified document and setting the values into the original document.Q : What will be the proper way to update a single document in the new (Mongo Java driver 4.7.0)
-> You should be using
updateOne(query, updates, options)
to update a single document on a MongoCollection object.From updateOne docs :
Q : Is it seems like it was an attempt to do UPSERT operation (Update or Insert) ?
-> Yes, it’s an upsert operation.
Q : Why the code is trying to remove
_id
from document ?-> The
update
method will update the document if the document was found byinternal_id
. If the document was not found and also if there is no_id
field in the document, then the mongoshell will consider it as a new document and will invokeinsert
method internally via theupdate
method to insert the document. For the insertion to happen, that’s why it was removed from document.Just update the code to this.