Sunday, 15 September 2013

reactivemongo - merging two BSONDocuments

reactivemongo - merging two BSONDocuments

I am looking for the most efficient and easy way to merge two BSON
Documents. In case of collisions I have already handlers, for example if
both documents include Integer, I will sum that, if a string also, if
array then will add elements of the other one, etc.
However due to BSONDocument immutable nature it is almost impossible to do
something with it. What would be the easiest and fastest way to do
merging?
I need to merge the following for example:
{
"2013": {
"09": {
value: 23
}
}
}
{
"2013": {
"09": {
value: 13
},
"08": {
value: 1
}
}
}
And the final document would be:
{
"2013": {
"09": {
value: 36
},
"08": {
value: 1
}
}
}
There is a method in BSONDocument.add, however it doesn't check
uniqueness, it means I would have at the end 2 BSON documents with "2013"
as a root key, etc.
Thank you!

No comments:

Post a Comment