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

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


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

In above code, Im trying to update subset of attributes in a document. After update I see whole document is replaced with just the subset. Is there a way to update a subset of attributes using bulkwrite operations using mongo-java-driver.

2

2 Answers

0
votes

If you want to update some field only, don't set the whole object:

new Document().append("$set", document)));

Instead, set fields you need only:

new Document().append("$set", new BasicDBObject("field1",document.getField1()).append("field2", document.getField2());
0
votes

UpdateOneModel updates as expected, I was infact populating null values to other attributes, thats the reason other attributes were getting updated to null.