Workaround to model nested data in mongodb

@edupardo I’ve needed something similar also, thinking of it as multi-level objects, not just a flat object shape. Actually, I currently have this working in production using a patch to Strapi, of course this is very fragile and breaks with some Strapi updates.

Here’s what I do:

  • I use my custom database conenctor for Firestore database, Firestore is a NoSQL database which supports multi-level JSON objects/documents. I haven’t used MongoDB at all, but from what you’re saying it may natively support the same thing, however Strapi’s Mongo connector may not… I have no idea - you’d be on your own there.
  • I define attributes in the model JSON with dot paths. Suprisingly this mostly just works! However it requires some patches to Strapi’s validation logic for handling recursive validation of multi-level objects.
  • I have a fork/patch here, but like I say, it’s fragile! You’d be on your own here unless I can get a PR into Strapi… @DMehaffy would you even consider a such PR, if the functionality may not actually be usable with the official connectors?

You may define your model like so, for example:

{
  "attributes": {
    "name.first": {
      "type": "string"
    },
    "name.last": {
      "type": "string"
    }
  }
}

If you can get it all working and linked together, it results in data like:

{
  name: {
    first: 'John',
    last: 'Doe'
  }
}
1 Like