Chapter - 3: Applying Subset Pattern Lab - getting correct code without using proper schema for it

hello community :slight_smile:

just completed this chapter - 3 final lab, where we have to apply ‘subset pattern’ to get valid code for this lab.

but i dont understand why is just by simply using “data type - array” for "“top_five_reviews” and “top_five_questions” gave me correct validation code?! and not had to use any ‘underlying schema within them’!!

please feel free to share your response about this, or any feedback rather

thanks you for looking into this, much appreciated and happy data modeling :slight_smile:

MongoDB documents are flexible, meaning you may or may not have a field in the document.

As for the schema, it is an interface to your programs about what fields you expect from a document.

Adding “top_five_” fields are required to have, but removing other fields is not. Those are assumed to be removed from the schema so the documents retrieved would not exceed the RAM limit. Those large fields are also assumed to be moved to their respective databases by DB admin, and queries would then use “$lookup” operations to get them as needed.

1 Like

thanks for your feedback :slight_smile:

i bet you’re saying what is correct but im not quite fluent in mongodb as of yet, so it’s not making full sense to me about ‘RAM’ limit reference that you brought in here!!

my point query here was: i was able to get correct validation just by using field names: [] and didnt have to define ‘any schema’ in it!! hope this makes sense…

i was worried if there was any ‘bug’ in schema validator or something :slight_smile:

the schema file, and all documents in MongoDB, uses BSON file format and it is inherited from the Javascript and JSON data structure. you have “number/string/boolean” as atomic types and “object” and “array” as composites. object is defined/created simply by curly braces { } and array by square brackets [ ]. BSON adds “ObjectId”, “decimal” and some other types.

in the schema, you have 3 ways to use a type: an object by { }, an array by [ ] and other type names in angle brackets within quotes, "<type>". you can then, if you need more precision, make composites by defining more inside the object, such as "address":{"city":"<string>","country":"<string>"}, and array, such as ["<string>"]

now back to the validator. you can evolve your schema from an empty file if you want to trace how it works, and also improve your understanding on schema design. In each step below, use validate_m320 pattern_subset --file empty.json --verbose

  • create an empty JSON file and add only {}. validator will tell you which fields are needed: (root): _id is required
  • add each field but with empty type "_id":"". validator will tell you which data type you need for that field: _id: _id must be one of the following: <objectId>
  • add an extra field that was not listed as required, "extra_field":"". validator will tell you to remove it: (root): Additional property extra_field is not allowed
  • if you define the type to be {} (object) you have to fill in the properties of that object. "available_sizes":{} will tell you available_sizes: mens is required ...
  • you will have no error if your type is an empty array, [ ], because all you need to know is that this one is an array. you have to define the type of array, only when you connect to an application because the app has to know what will it get beforehand.
    • now, if you try to add an empty type in it, "top_five_reviews":[""], you will get top_five_reviews.0: Invalid type. Expected: object, given: string
    • and then if you try an empty object in the array "top_five_reviews":[{}], you will reach a deeper error top_five_reviews.0: author is required

So the array type is the one you need to pay attention to at all times. Unlike relational databases, MongoDB documents are “flexible” so your array does not have to have a concrete shape. but if you want a shape, then it goes deeper because an array is a source for infinitely sized data. also keeping the data type the same across all documents is the duty of the programmer.

1 Like

thanks for confirming what i was actually guessing about it. much appreciated you taking this time and explaining this to me and thus rest of community, have a nice day and happy data modeling :slight_smile:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.