Skip to content
Advertisement

Returning specific fields with mongoose

I’m trying to accomplish something really easy but still manage to fail.

What I am trying to do is when I get a get request on my server I want to return all documents BUT just the specific fields populated.

My schema goes as follows

JavaScript

What I want is to return only client.phone and client.email plus orderdetails.status but still retain reference field if possible

I have tried using lean() and populate() but had no luck with them. Is there anything utterly simple I am missing? Or what I am trying to achieve is not that easy? Thanks!

Advertisement

Answer

You can specify the fields to return like this:

JavaScript

Alternative syntax

JavaScript

I’ve made a number of assumptions here, but you should be able to see the idea.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement