Migration and programming logic

Hello everyone,

I have a basic logic question. The solution shows the predicate checking the existence of “lastupdated” and then checking to ensure it is a type string. However, I wrote the predicate to only return “lastupdated” with type string. If something is a type string, then it exists, right?

Since null is it’s own type, “lastupdated” entries that are null will still exist, but won’t match to type string.

So, do we really need ensure “lastupdated” exists and has type string, or is my method okay?

Thanks

Right you are.

Double-check the Test script to ensure that it’s not testing for an exists check as well.

2 Likes

007_jb,

I didn’t have any issues with the test and the mflix application status. The test scripts simple checks one document to ensure the lastupdated is a datatime type (as best as I can understand flask).

But I think my question is more of a NoSQL question. Since the schema isn’t defined, any document may or may not have a lastupdated field. And, that field could be different types in different documents. So, with a mongodb query, is there any reason to query for documents for which both a field exists and that field is a specific type.

I understood your question that was why I concurred with you in my last post :slight_smile:

The use of $exists in conjunction with $type in the lab is not necessary like you rightly suggested. In fact, a singular $type check may be more performant than a dual $exist + $type checks. However, there might be performance gains if the indexed field is a Partial Index whose expression is an existence check.

It’s possible that the tutors decided to utilise $exists as a way of introducing readers to this very useful operator.

1 Like

007_jb:
Thanks again for the followup. I haven’t really looked at indexing yet (I am new to this whole thing). That does seem like a great use case for doing the $exists check.

Right now I’l considering small data bases, so haven’t thought much of indexing. But, if there is any potential of the database expanding, it would probably be best to bake indexing in early, rather than trying to retrofit.

Something to add to my learning queue. :slightly_smiling_face:

Agreed… plan ahead :slight_smile:

Enjoy the rest of the course(s).