DEV Community

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

Posted on

7 5

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.

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (1)

Collapse
 
jdrumgoole profile image
Joe Drumgoole

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

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay