Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save rokinmaharjan/505108a038d4c8d591011d46c37f7957 to your computer and use it in GitHub Desktop.

Select an option

Save rokinmaharjan/505108a038d4c8d591011d46c37f7957 to your computer and use it in GitHub Desktop.
Updating multi-level nested document in MongoDB - Spring
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