Lab: Apply the Polymorphic Pattern What is the issue?

Hello,
I am blocked on this lab, I have seen many discussions on this lab such as Apply the Polymorphic Pattern Lab - #13 by steevej.

Unfortunately, I don’t get why my code does not pass the validation.

Could someone help me on this?

[{"_id": "<objectId>",
"product_id": "<int>",
"title": "<string>",
"authors": ["<string>"],
"description": "<string>",
"publisher": "<string>",
"language": "<string>",
"catalogues": {
    "isbn10": "<string>",
    "isbn13": "<string>"
  },
"format":[{"book":{
    "pages": "<int>"
}},
  {"ebook":{
      "epub": {
        "pages": "<int>"
      },
      "pdf": {
        "pages": "<int>"
      }
    }},
  {"audiobook":{
    "narrator": "<string>",
    "length_minutes": "<int>"
  }}]
  }]

Hi @Han_Heloir,

What error you are getting, could you please share the screenshot of the same?

Thanks,
Kushagra

Hello Kushagra,
I have got this error `

Answer Filename: /Users/ Google Drive/My Drive/Mongo Training/Mongo University/M320/pattern_polymorphic.json

Processing document: 1

  ... against solution: 1
The document fails validation.

Errors:
At least one of your solutions is not passing validation.

@Kushagra_Kesav

Hi @Han_Heloir,

Could you kindly try this:

[{
  "_id": "<objectId>",
  "format": "<string>",
  "product_id": "<int>",
  "title": "<string>",
  "description": "<string>",
  "authors": ["<string>"],
  "publisher": "<string>",
  "language": "<string>",
  "pages": "<int>",
  "catalogues": {
    "isbn10": "<string>",
    "isbn13": "<string>"
  }
}, {
  "_id": "<objectId>",
  "format": "<string>",
  "product_id": "<int>",
  "title": "<string>",
  "desc": "<string>",
  "description": "<string>",
  "authors": ["<string>"],
  "publisher": "<string>",
  "language": "<string>",
  "eformats": {
    "epub": {
      "pages": "<int>"
    },
    "pdf": {
      "pages": "<int>"
    }
  },
  "isbn10": "<string>"
},{
  "_id": "<objectId>",
  "format": "<string>",
  "product_id": "<int>",
  "title": "<string>",
  "desc": "<string>",
  "description": "<string>",
  "authors": ["<string>"],
  "narrator": "<string>",
  "publisher": "<string>",
  "language": "<string>",
  "length_minutes": "<int>"
}]

Let us know if it works for you or not?

Thanks,
Kushagra

Hello Kushagra,
It works, but what I don’t understand is that is not the right answer, isn’t it?

Thanks,

Han

Hey @Han_Heloir,

As was given in the problem statement, you had the modify the following schemas:

[{
  "_id": "<objectId>",
  "product_id": "<int>",
  "title": "<string>",
  "description": "<string>",
  "authors": ["<string>"],
  "publisher": "<string>",
  "language": "<string>",
  "pages": "<int>",
  "catalogues": {
    "isbn10": "<string>",
    "isbn13": "<string>"
  }
}, {
  "_id": "<objectId>",
  "title": "<string>",
  "desc": "<string>",
  "authors": ["<string>"],
  "publisher": "<string>",
  "language": "<string>",
  "eformats": {
    "epub": {
      "pages": "<int>"
    },
    "pdf": {
      "pages": "<int>"
    }
  },
  "isbn10": "<string>"
},{
  "_id": "<objectId>",
  "title": "<string>",
  "desc": "<string>",
  "author": "<string>",
  "narrator": "<string>",
  "publisher": "<string>",
  "language": "<string>",
  "length_minutes": "<int>"
}]

Your solution did not fully implement polymorphic pattern. I would recommend watching the lecture video again and reading the instructions of the lab.

As per instructions given in the lab, a learner is supposed to:

We will make the following modifications to our 
current schemas to have all documents share some 
common elements:

Across all documents, we will use a new field 
called format that will have a value of book, ebook, or audiobook.
Across all documents, we will have a field called
 product_id that will accept an integer as its value.
The field description should be unified across all documents.
The field authors should be unified across all documents.

Hence, for this lab, firstly we create a field called format. In this field, we put one of the strings book, ebook, or audiobook.

Secondly, we need to add the field product_id to the ebook and audiobook documents.

Third, we want to unify the description across the documents. There are two ways we can do that. The first is to rename the field desc to the description for the ebook and audiobook. An alternative is to make a copy of the contents of desc into description to keep some backward compatibility. Both solutions would work.

Finally, for the authors, we need to turn the author of the audiobook documents from a string to an array of strings.

Since you did not fully implement what the instructions said, the validator gave you The document fails validation warning, but generated the validation code for @Kushagra_Kesav’s solution.

Hope this helps. Feel free to reach out if there is anything else as well.

Regards,
Satyam

1 Like

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