DEV Community

porgeet
porgeet

Posted on

Help With Interrelated Data Files for MongoDB?

Hi all 👋

I have been wrestling with this issue for ages and getting slightly closer, but the progress is painfully slow. Hopefully someone can help me... 🙏

How can I write interrelated data populate a mongodb database?

Here's an example, I have a series of JavaScript files:

Weapon File - (to go to weapon collection)

[
    {
    name: "Sword",
    damage: 8,
    damageType: ObjectID
    }
]

Damage Type File (to go to damage type collection)

[
    {
    name: "Slicing",
    weapons: [ObjectID, ObjectID]
    }
]

Where ObjectID would refer to the _id of the interrelated data. In this case a weapon would have a damageType and a Damage Type would have a series of weapons.

How would I write those javascript files since the _id field is generated by mongodb?

Any help would be greatly appreciated 😄

Cheers
Pete

Top comments (0)