Chrillewoodz
Chrillewoodz

Reputation: 28318

MongoDB not returning empty objects in document

I've got this JSON snippet in a much much larger MongoDB document:

formConfig: {
  person: {},
  title: {name: 'title', ... },
  name: {name: 'name', ...}
}

However, when I then try to retrieve the document containing this JSON it doesn't return person: {} at all. All I'm getting is:

formConfig: {
  title: {name: 'title', ... },
  name: {name: 'name', ...}
}

Which completely breaks the frontend side of things since I need to know that person is there, regardless if it's empty or not.

When I search for this issue I can't find any similar questions or resources explaining why this happens in the first place, let alone how I can fix it.

So how do I fix it so it returns the person as well?

Here's the actual query, appConfig contains the JSON as mentioned:

exports.getSingle = (req, res, next) => {

  AppConfig.findOne({_id: req.params.id})
    .exec((err, appConfig) => {

      res.json({
        error: null,
        data: appConfig
      });
    }
  );
};

Upvotes: 2

Views: 597

Answers (1)

Chrillewoodz
Chrillewoodz

Reputation: 28318

The issue was actually at Schema level, not using minimize: false in the Schema options cause empty objects to be removed.

This works:

new Schema ({...}, {minimize: false});

Upvotes: 2

Related Questions