E
E
Evgeny Zhurov2021-08-02 22:42:16
MongoDB
Evgeny Zhurov, 2021-08-02 22:42:16

How to store JSON in MongoDB while maintaining associations?

I'm trying to migrate a sql dump to MongoDB. I overtook this case in json, slightly changed the structure, and I try to save it from the file to the database. The problem is that the id of the records in the dump were purely numeric, and as such, they are not suitable as an ObjectId . And I need to save links between different types of posts and categories. Relationships are set precisely through these numerical IDs.
I tried to set _id at the time of saving in this form:

new ObjectId(x.toString(2))

или
new ObjectId(x.toString(16))

или просто
x.toString(2)

but doesn't work. Is there some way to convert the numeric id to what mongodb requires? Namely:

Argument passed in must be a single String of 12 bytes or a string of 24 hex characters


Or maybe there are other options?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2021-08-03
@Zhuroff

Or maybe there are other options?

There is. Use each DBMS for its intended purpose and do not try to push relational data into a document database.
In general, it’s probably worth just generating new ObjectIds for independent entities, and dragging them into all dependent entities instead of the corresponding id. Well, i.e. no need to try to solve the problem head on, trying to convert one id to another, you need to convert the RELATIONSHIP, not the id. To convert a relationship, you don't need the original id, it's important for you to connect with the new ObjectId the same entities that were connected in the relational database by the original id.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question