Scenario
Example Mongo DB documents
Doc 1
{
_id 123
Customer: customer A
State: TX
City: Houston
checksum: 1111
IsBundlled:
}
Doc 2
{
_id 1234
Customer: customer B
State: TX
City: Houston
checksum: 1111
IsBundlled:
}
Doc 3
{
_id 12345
Customer: customer c
State: NY
City: Buffalo
checksum: 2222
IsBundlled:
}
Checksum field is populated in app code using State and City fields.
Problem
Populate IsBundled field for each document to true, if a checksum matches more than 1 documents. In our case 1st and 2nd documents needs to updated with isBundled: true . Instead of adding this logic in application code, I am looking for better solutions using aggregation pipeline
2
Answers
Maybe something like this:
aggregation playground
Explained:
MongoDb Playground link : https://mongoplayground.net/p/pV_UbQ-ADby
$group:
To group the matching documents.$set:
To modify the document$gt:
To check if greater than condition$replaceRoot:
To replace the current document structure by another.