DEV Community

Cover image for How to get only required data fields from mongoDB
Mamun Abdullah
Mamun Abdullah

Posted on

How to get only required data fields from mongoDB

If you are looking for a solution to get only the required fields data from mongoDB, this simple solution can help you without any side effect

Say, you have this data fields in your database

        meetingId,
        meetingDate,
        noticeDate,
        title,
        agenda,
        venue,
        notice,
        noticeDistribution,
        chairedBy,
        participants,
        minutes,
        minutesPreparedBy,
        minutesApprovedBy,
        minutesDistribuion,
        status,
        username,
        userid
Enter fullscreen mode Exit fullscreen mode

But you need to send only minutes and minutesApprovedBy fields, then you can go this way,

yourDB.find({}, {minutes:true, minutesApprovedBy:true})
.then(data=>res.send(data))
.catch(err=>res.send(err))
Enter fullscreen mode Exit fullscreen mode

The field you need, put together with a value to true as shown above. This will return only those fields (filter) from the database. You can also use findOne({}, {}) where necessary.

Top comments (1)

Collapse
 
jdrumgoole profile image
Joe Drumgoole

Full docs for projection are at docs.mongodb.com/manual/tutorial/p...