Search code examples
pythonmongodbmongoengineflask-mongoengine

What is the best choice of field for this project, using MongoEngine?


im currently working on a project and was wondering if i could get a little bit of advice. I aim to be storing information about a number of URLs, which will each have a number of parameters and will look something like this:

{
  Name: "Report1",
  Host: "127.0.0.1",
  Description: "This is a test report",
  Date: "00/00/00",
  Time: "00:00:00",
  Pages:{
        Page: "test_page_url",
        Parameters:{
                 Parameter: "test_param",
                 Value: "test_parm_value"
                    }

        }

}

I've not been able to find much information/ examples of using a one-to-many, within a one-to-many relationship using MongoEngine and was wondering what the best approach would be? Is it possible to use EmbeddedDocumentListField in this manner or would it be best practice to use ReferenceField. Any advice would be greatly appreciated as im still quite new to the NoSQL approach


Solution

  • As a simple rule, embedded documents are well suited if

    • a Parameter belongs to only one Page and a Page and its Parameters belongs to only one test

    • your document won't grow over 16 Mo.

    AFAIU, this is the case here, so I don't see any reason to use ReferenceFields, which would introduce complexity and performance penalty.

    To put it simply, if you can represent your data as you did in your question, laid out as a single document rather than linking several documents together like in a relational model, then its probably safe to use embedded documents.

    More details here as suggested in a comment.