1

I'm not a Mongo expert. I'm trying to update the complete document by replacing with the new one.

I have the following fields in the existing document in the collection

qid, column a, column b, column c

The new document has the following fields

qid, column a, column b, column d, column e (column c is not available)

So, when I try to update the document with the below code, it gets updated as following in the document in the collection

qid, column a, column b, column c, column d, column e

List<WriteModel> updateList = new ArrayList<WriteModel>(documents.size());

documents.stream().forEach((document) -> {
    updateList.add(new UpdateOneModel<Document>(
            new Document().append("qid", qid),
            new Document().append("$set", document)));
});


BulkWriteResult result = securitiesCollection.bulkWrite(updateList,
        MongoDbConstants.ORDERED_OPTION_FALSE);

How can I update the complete new document set in the collection by removing the existing document?

Syed
  • 2,471
  • 10
  • 49
  • 89
  • See this answer https://stackoverflow.com/questions/50627595/updateonemodel-replaces-existing-document-in-mongodb-java-driver . You need to call new Document().append("$set", document))); to replace the whole document – Orçun Çolak Feb 22 '23 at 13:55

0 Answers0