MonogoDB findById() is a very popular data finding query. We can easily get all the data using an _id in MongoDB. Let's look an example

If we have such documents in our database collection named Users

[
  {
  "_id":ObjectId("5ebadc45a99bde77b2efc16a"),
  "username":"tradecoder",
  "userType":"admin",
  "email":"tradecoderbd@gmail.com",
  "firstName":"trade",
  "lastName":"coder",
  "password":"12345678",
  "createdAt":"2020-12-13T16:39:30.385Z",
  "updatedAt":"2020-12-13T16:39:30.385Z"
  },
  {
  "_id":ObjectId("5ebadc45a99bde77b2efc16b"),
  "username":"user2",
  "userType":"editor",
  "email":"user2@example.com",
  "firstName":"first name",
  "lastName":"last name",
  "password":"12345678",
  "createdAt":"2020-12-14T16:39:30.385Z",
  "updatedAt":"2020-12-14T16:39:30.385Z"
  },
  {
  "_id":ObjectId("5ebadc45a99bde77b2efc16c"),
  "username":"user3",
  "userType":"author",
  "email":"user3@example.com",
  "firstName":"first name",
  "lastName":"last name",
  "password":"12345678",
  "createdAt":"2020-12-15T16:39:30.385Z",
  "updatedAt":"2020-12-15T16:39:30.385Z"
  }
]

If we have many documents like this in our database, and we want to get a specific document, we can easily get our expected doucment by its _id.
Here is the code with mongodb findById() query, it will return the complete document relevant to that id.

Note:

If you use mongoose Schema model, and named it Users you can use directly Users.findById(), or if you are not using mongoose Schema model, you need to follow this syntax db.collection.findById(), and if your collection name is Users you have to code like this db.collection("Users").findById() where db is your mongodb connection. Here, we are using mongoose Schema model. Here is the code to get your data

const id = "5ebadc45a99bde77b2efc16b";
Users.findById(id)
.then(data=>console.lo(data))
.catch(err=>console.log(err))

This will return the second document as the id matches with the second one and the output is

{
"_id":ObjectId("5ebadc45a99bde77b2efc16b"),
"username":"user2",
"userType":"editor",
"email":"user2@example.com",
"firstName":"first name",
"lastName":"last name",
"password":"12345678",
"createdAt":"2020-12-14T16:39:30.385Z",
"updatedAt":"2020-12-14T16:39:30.385Z"
}

Node, express, mongodb code example

Here router is express router
const router = require('express').Router();
and Users is mongodb collection, generated with mongoose Schema model
Here we are sending an id through req.params to get the data

router.rotue('/api/user/:id')
.get((req, res)=>{
const {id}=req.params;
Users.findById(id)
.then(data=>res.send(data))
.catch(err=>res.send(err))
})

And if the id sent with a vlue to "5ebadc45a99bde77b2efc16a", our response data will be this one, because it finds/matches our first document

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

So, as you see this is a very simple process to work with MongoDB.

Now if we need only the username, or only firstName, or only username and email, or all the data fields but not the password, then what to do?
It's also a very simple process, check it here with tradecoder.

Read more...

Go back to MongoDB section.


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