Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Javascript 

Mongoose/Mongo cloning related and subdocuments

Really hoping some of my mongodb/mongoose rude boyz and gangstas can help me out.

I need to clone a document in my mongodb, such that it's just a complete copy of the original. I've written a query to fetch the document back, which seems fine. I then took some node code I found online which strips out the id etc, and can insert it in my db correctly.

Code
copyDoc._doc._id = mongoose.Types.ObjectId()
copyDoc.name = 'Copy ' + copyDoc.name
copyDoc.isNew = true

const doc = await Model.create(copyDoc)

However.

The original document will have some subdocuments, and some related documents in other collections. They need to be copied too, and the id numbers stripped out and replaced (and the foreign key needs to be the new copied document, not the original). They're populated when I fetch the document, but I can't figure how to copy them as well.

Is there a way of doing this with a query? Or do I need to interrogate each collection in turn, seek out the foreign key, and copy each document in a forEach loop or something? This seems very complicated and inefficient (eg what if I add a new collection?)

Is there some kind of mongoose/mongo query, involving that '$unwind' and '$unset' business, maybe, where you can whip through several collections and pull out any documents with the correct foreign key, strip out their id, and copy them?

Thanks.
It's been a while since I've worked with mongoose, but a very simple idea would be to deep clone the document with everything (using either lodashs cloneDeep or a simple JSON.parse(JSON.stringify(doc)) and use a (recursive) loop, to remove all IDs.

Not totally sure how to handle fkeys without additional logic.
Pigeon Chess

“What is infinite? The universe and the greed of men.”
(Dec 26, 2021 12:48 PM)T0Bi Wrote:  It's been a while since I've worked with mongoose, but a very simple idea would be to deep clone the document with everything (using either lodashs cloneDeep or a simple JSON.parse(JSON.stringify(doc)) and use a (recursive) loop, to remove all IDs.

Not totally sure how to handle fkeys without additional logic.

Thanks, yeah, I feel like there might be some mongo/mongoose query that I could use, I already have some byzantine beauties in my code that I wrote ages ago that I now have have only the vaguest idea what they're doing, they always have operators like '$unwind' and '$unset'.

Mongo seems oddly abstract to me compared to sql, I'dve thought this would be quite straightforward to do, as it seems like something you'd want to do regularly, but I have a feeling that I have to add a foreign key 'manually' to the related document(s), so presumably to copy the document and reassign the foreign key, it'll be necessary to do that 'manually' as well.
(Dec 26, 2021 01:50 PM)bevc Wrote:  Thanks, yeah, I feel like there might be some mongo/mongoose query that I could use, I already have some byzantine beauties in my code that I wrote ages ago that I now have have only the vaguest idea what they're doing, they always have operators like '$unwind' and '$unset'.

Mongo seems oddly abstract to me compared to sql, I'dve thought this would be quite straightforward to do, as it seems like something you'd want to do regularly, but I have a feeling that I have to add a foreign key 'manually' to the related document(s), so presumably to copy the document and reassign the foreign key, it'll be necessary to do that 'manually' as well.

SQL and noSQL databases aren't really comparable. They have their different use cases.
You shouldn't try to replicate SQL behavior on with a mongodb.

And tbh, I've never had to clone a document with all it's subdocuments etc. and after some quick googling, seems like nobody else has this problem :D
Pigeon Chess

“What is infinite? The universe and the greed of men.”
(Dec 26, 2021 03:21 PM)T0Bi Wrote:  SQL and noSQL databases aren't really comparable. They have their different use cases. 
You shouldn't try to replicate SQL behavior on with a mongodb. 

And tbh, I've never had to clone a document with all it's subdocuments etc. and after some quick googling, seems like nobody else has this problem :D

Thanks brother, yeah, I know, it's weird how some shizz that seems so easy in sql, you think it's going to be easy in nosql too, and vice versa.

I couldn't find much by googling either, I would have thought this was an issue that came up all the time, but clearly not! I guess I'm gonna have to write queries to extract the documents from each collection by the foreign key, loop through that shit and copy and re-assign it all and insert it. The worst part is having to explain to my non-technical colleagues why something that seems super-easy ("it just needs to copy and paste, like a word doc"), is in fact, a complete ballache.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Javascript]  by bevcMar 25, 2022 02:34 AM 5 218 Mar 25, 2022 02:34 AM
Last Post: bevc
[Javascript]  by bevcMar 17, 2021 03:41 PM 6 299 Mar 17, 2021 03:41 PM
Last Post: Ghost.13
[Javascript]  by bevcMar 22, 2020 10:08 AM 0 84 Mar 22, 2020 10:08 AM
Last Post: bevc