You know that a simple query with mongodb returns all the fields for the matched documents.
But, you also can control it in a very simple process to get data only what you need.
Let's see an example, here is a document in our database in Users collection

Example 1


  {
  "_id":ObjectId("5ebadc45a99bde77b2efc16a"),
  "username":"tradecoder",
  "userType":"admin",
  "email":"tradecoderbd@gmail.com",
  "firstName":"trade",
  "lastName":"coder",
  "fullName":"trade coder",
  "password":"12345678",
  "createdAt":"2020-12-13T16:39:30.385Z",
  "updatedAt":"2020-12-13T16:39:30.385Z"
  }

So, when you send a request to get data, say findById()

const id="5ebadc45a99bde77b2efc16a";
Users.findById(id)

This will return the matched document with all the fields as shown above. Now, if we need only the username from the document including its _id, we can make it this way,

Users.findById(id, {username:true})

The out put is

{
"_id":"5ebadc45a99bde77b2efc16a",
"username":"tradecoder"
}

And if we need username and email, we can make it this way

Users.findById(id, {username:true, email:true})

This will return

{
"_id":ObjectId("5ebadc45a99bde77b2efc16a"),
  "username":"tradecoder",
  "email":"tradecoderbd@gmail.com"
  }

So, you must notice that we did not want the _id field but for the both case we are getting our expected fileds but it also includes _id field. And now if we really don't want to send that _id field, then we can make it this way

Users.findById(id, {username:true, email:true, _id:false});
// we can replace true to 1, and false to 0 also for getting our required data
Users.findById(id, {username:1, email:1, _id:0});

Now, the output is

{
"username":"tradecoder",
"email":"tradecoderbd@gmail.com"
}

So, you have noticed that we can exclude any field by giving it a value false or zero 0. Now, think, what the code will be if we want all the fields to send but not the password! Easy? yes, here is the code

Users.findById(id, {password:0})

And now the output is all the fields but not the password :

 {
  "_id":ObjectId("5ebadc45a99bde77b2efc16a"),
  "username":"tradecoder",
  "userType":"admin",
  "email":"tradecoderbd@gmail.com",
  "firstName":"trade",
  "lastName":"coder",
  "fullName":"trade coder",
  "createdAt":"2020-12-13T16:39:30.385Z",
  "updatedAt":"2020-12-13T16:39:30.385Z"
  }

Example 2

So far we are using findById(). We also can find a single document by using findOne()

// find by _id
const id = "5ebadc45a99bde77b2efc16a"
Users.findOne({_id:id});

//or find by username
const user = "tradecoder";
Users.findOne({username:user});

//or find by both _id and username and whatever you want to ensure to match
Users.findOne({_id:id, username:user})

All the above will return the matched full document

{
  "_id":ObjectId("5ebadc45a99bde77b2efc16a"),
  "username":"tradecoder",
  "userType":"admin",
  "email":"tradecoderbd@gmail.com",
  "firstName":"trade",
  "lastName":"coder",
  "fullName":"trade coder",
  "password":"12345678",
  "createdAt":"2020-12-13T16:39:30.385Z",
  "updatedAt":"2020-12-13T16:39:30.385Z"
  }

And like in Example 1 we can include or exclude our specified fields with this

Users.findOne({_id:id, username:user}, {username:1, fullName:1, _id:0})

The output is

{
"username":"tradecoder",
"fullName":"trade coder"
}

Use of return data in your node, express app

Users.findOne({_id:id, username:user}, {username:1, fullName:1, _id:0})
.then(data=>data?res.send(data):res.send("Not found!"))
.catch(err=>res.send(err))

By this your server will send data includingusername and fullName and do what you want with this.

You also may like

Read more on TradeCoder : MongoDB Section or find your MongoDB code solution


If you find this document helpful, please feel free to share it on your networks...