Here is my data structure.
[{ \"name\": \"David\", \"lastname\": \"\", }, { \"name\": \"Angela\" }]
\"lastname\" is sometimes present and
You can use a regex query:
db.test.find({ "lastname": /(.|\s)*\S(.|\s)*/ })
This regex matches strings beginning or ending with 0 or N whitespaces (.|\s) and it have to be one or more non-whitespaces \S in the middle.
(.|\s)
\S