Merge Multiple Documents in realm

Hi Team,

I have below three documents retrieved from three collections -
{ fulldocument }
{“Key1”:“value1”,“key2”:“value2”}
{“key3”:“value3”,“key4”:“value4”}

Need to merge in one and insert in elastic search like below -
{ fulldocument,
“Key1”:“value1”,
“key2”:“value2”,
“key3”:“value3”,
“key4”:“value4”}

I am not able to merge it in one document.
I can do it by adding one by one attribute but want to add it as document.

Thanks,

Hi @gopinath_maheswaran,

Check the option of running aggregation with $mergeObjects:
https://docs.mongodb.com/manual/reference/operator/aggregation/mergeObjects/#mergeobjects-aggregation

Potentially you can use $unionWith from the three collection and group by null. Last stage can be the merge objects.

Best
Pavel

Thanks for the suggestion!,

I tried multiple options,

  1. $mergeObject
    output - {$mergeObject: { fulldocument},
    {“Key1”:“value1”,
    “key2”:“value2”},
    {“key3”:“value3”},
    “key4”:“value4”}}

root is creating with $mergeObject name and it’s list of object in one object.

  1. Object.assign -
    output -{$set: { fulldocument,
    “Key1”:“value1”,
    “key2”:“value2”,
    “key3”:“value3”,
    “key4”:“value4”}}

root is creating with $set.

but in both scenario after data insert in elastic search output for all the integer data is like below -
“downloads”: {
“$numberInt”: “10”
}

expected result - “downloads”: 10

Elastic search output is working expected if data is inserting through postman.

Thanks,

This is expected as it is Extended Json represention…

How do you fetch the data ? Is it through a Driver or a Realm function? (Now noticed Realm case type)

Of its from a function or a webhook you need to JSON.stringify output :

docs = await coll.find()
response.setBody(JSON.stringify(docs))

Thanks
Pavel