Document.prototype.$isEmpty()

Returns:
  • «Boolean»

Returns true if the given path is nullish or only contains empty objects. Useful for determining whether this subdoc will get stripped out by the minimize option.

Example:

  1. const schema = new Schema({ nested: { foo: String } });
  2. const Model = mongoose.model('Test', schema);
  3. const doc = new Model({});
  4. doc.$isEmpty('nested'); // true
  5. doc.nested.$isEmpty(); // true
  6. doc.nested.foo = 'bar';
  7. doc.$isEmpty('nested'); // false
  8. doc.nested.$isEmpty(); // false