Merge operation?

2 views
Skip to first unread message

stephanos

unread,
Oct 31, 2010, 10:17:37 AM10/31/10
to mongo-scala-driver
I was wondering if there is some solution for merging datasets (based
on same oid) - either directly by mongodb or your cool wrapper?
Dropping the dataset and then re-inserting seems wrong...

PS: Any chance you'd put the 0.8.7 release on Maven soon?

Cheers :)

stephanos

unread,
Oct 31, 2010, 10:21:28 AM10/31/10
to mongo-scala-driver
Just to be clear: I have a dataset in the DB with id X and data XYZ.
Now I want to write a new dataset to the DB which has the same id (X)
but data XYY.

Obviously if I simply save() it would create a new entry. And if I
were to use update() I'd have to write a update command for each
field...

Alexander Azarov

unread,
Nov 3, 2010, 6:55:02 AM11/3/10
to mongod...@googlegroups.com
You just get XYZ from one dataset, update the fields, add new ones from XYY (keeping _id from XYZ) and save(). Why wouldn't this work?

Yes, I'll release 0.8.7 soon. Thanks for reminding.

Regards,
Alexander


31.10.2010, в 17:21, stephanos написал(а):

stephanos

unread,
Nov 3, 2010, 12:40:07 PM11/3/10
to mongo-scala-driver
Sorry, my example wasn't really good.

Let's assume we have the document 'ExampleDoc':
ExampleDoc (id, name, type, date) [with a unique index on id]

Now we persist Doc1(1, "example", "string", "01-01-2010") to the
database.

And then let's assume we need to update/merge this with Doc2:
Doc2(1, "example", "string", "30-12-2012") [date has changed]

I couldn't simply save Doc2 to the database because it has the same id
as Doc1 (->error).
I want to "overwrite" the Doc1.

BUT
...removing it and then persisting again seems unreasonable.
...updating each field manually isn't doable when this is required for
many documents.

PS: Do you know JPA/Hibernate? There it would be called 'merge'.

What's your approach?


On Nov 3, 10:55 am, Alexander Azarov <aza...@aha.ru> wrote:
> You just get XYZ from one dataset, update the fields, add new ones from XYY (keeping _id from XYZ) and save(). Why wouldn't this work?
>
> Yes, I'll release 0.8.7 soon. Thanks for reminding.
>
> Regards,
> Alexander
>
> 31.10.2010, × 17:21, stephanos ÎÁÐÉÓÁÌ(Á):

Alexander Azarov

unread,
Nov 3, 2010, 4:42:11 PM11/3/10
to mongod...@googlegroups.com
As far as I understand, when you load a document, it gets cached in MongoDB, so saving it immediately is likely very fast. Thus the trivial approach "load" -> "modify" -> "save" per every document may be effective for MongoDB.

You could do update({_id: "OID"}, {$set: {field: new, ...}}, ...), but in the end it does the same, but on the server side. And furthemore, you will have to generate such "delta" description, which fields to set to new values and which to delete, etc.

BTW, when the size of your documents grow, this will slow the process significantly.

Your question is general, so I suggest you to ask at MongoDB-users mailing list, I am sure they will give you qualified answer.

03.11.2010, в 19:40, stephanos написал(а):

stephanos

unread,
Nov 4, 2010, 5:11:25 AM11/4/10
to mongo-scala-driver
Thanks for your answer - will do :)
Reply all
Reply to author
Forward
0 new messages