Created
November 28, 2018 09:06
-
-
Save rokinmaharjan/505108a038d4c8d591011d46c37f7957 to your computer and use it in GitHub Desktop.
Updating multi-level nested document in MongoDB - Spring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| EXAMPLE JSON: | |
| { | |
| "_id": 12345, | |
| "_class": "com.example.ProductRates", | |
| "rates": [ | |
| { | |
| "productId": NumberInt(1234), | |
| "rate": 100.0, | |
| "rateCardId": NumberInt(1), | |
| "month": NumberInt(201801) | |
| }, | |
| { | |
| "productId": NumberInt(1234), | |
| "rate": 200.0, | |
| "rateCardId": NumberInt(1), | |
| "month": NumberInt(201802) | |
| }, | |
| { | |
| "productId": NumberInt(1234), | |
| "rate": 400.0, | |
| "rateCardId": NumberInt(2), | |
| "month": NumberInt(201803) | |
| }, | |
| { | |
| "productId": NumberInt(1235), | |
| "rate": 500.0, | |
| "rateCardId": NumberInt(1), | |
| "month": NumberInt(201801) | |
| }, | |
| { | |
| "productId": NumberInt(1235), | |
| "rate": 234, | |
| "rateCardId": NumberInt(2), | |
| "month": NumberInt(201803) | |
| } | |
| ] | |
| } | |
| CODE: | |
| WriteResult wr = getMongoTemplate().updateMulti( | |
| new Query(where("rates.rateCardId").is(1234)), | |
| new Update().set("rates.$[item].rate", 200), | |
| new UpdateOptions() | |
| .arrayFilters( | |
| Arrays.asList( Filters.in("item.rateCardId",Arrays.asList(1,2)) ) | |
| ), | |
| ProductRates.class | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment