Workaround to model nested data in mongodb

Hey! Thank you for the great work you’re doing with Strapi! I have been playing around with it this weekend and it is amazing all the work I have been able to do in a very short time.

My data structure is a little bit complex and i want to embed different pieces of json inside of my main content type, for instance:

tournament = {
   headline: "Albatros Tennis League",
   minPlayers: 4,
   maxPlayers: 16,
   format: {
      name: "league",
      description: "Groups of 4 players in which everyone plays against everyone else..."
      (... more data ...)
   }
}

I have set the format as a component, but it is stored in another collection. As strapi doesn’t currently support MongoDB’s nested documents, i’m looking for a workaround to make this work.

I have thought something like using json types on Strapi and manage type validation by overwriting the model’s create component. It’s a shame because i really like the component architecture to manage this use case but i need the data to be nested, not in another collection.

Do you have any ideas on this approach as a workaround to embed data inside my main content types? Or can you think on another way to overcome it?

It should work with GraphQL!

Thank you so much! :slightly_smiling_face:

Currently we don’t support many MongoDB features and mostly treat mongo as a relational database. It’s to be determined (likely in Q3) if we plan to change this. Eventually we do plan to open up the strapi-connector- ecosystem to allow the community to build their own connectors if ours do not fit their needs.

Thank you for your reply!

It is shame. Opening the strapi connector ecosystem would be great. Thank you!

@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

That fork is my somewhat stale attempt to start a PR, I can’t remember what state I left it in. But here is the patch that I’m actually using. But it’s stuck a good few versions back, sorry.

1 Like

Hey @brettwillis, reading this now!

Thank you so much for your reply, it is very useful! It will serve me very well as a starting point.

I will work on it in the next few days :slightly_smiling_face:

MongoDb is working too fast instead of MySQL. I also convert my multiple webs database from SQL to MongoDb its giving too good response.

MongoDb is working too fast instead of MySQL. I also convert my multiple best tennis shoes wide feet webs database from SQL to MongoDb its giving too good response.