Mongoose is MongoDB driver module which provide REST features to build faster MongoDB apps with Nodejs.
Mongo Object Id
MongoDB object ID is an automatically generated multipurpose number. In our case we can use it to show up the document. In fact the Object ID consist of date and a unique random number.
Find records using MongoDB ObjectId
Post.findById(pid,function (error, post) {
if (error) return handleError(error)
if (post) {
console.log("Full post :" + post);
}
});
Using the findById method of mongoose model class allows us to find the document using MongoDB object Id and return result to the call back function in JavaScript style.
We can also use an asynchronous method find documents in Mongoose as follows
const post=await Post.findById(id)
and the resulted document will be
That’s it. Following mongoose posts deserve your attention
- How to export multiple components in Reactjs - How to render export multiple components in React
- How to interact with input in Reactjs - How to handle state and event of input element in React apps
- How to populate multiple MongoDB objects on the same path in express-node - How to populate multiple objects in MongoDB in the same path
- How to populate selected fields in a mongo document path in expressjs - How to populate object in mongo doc at a specific path with specific fields
- How to populate and display nested mongo objectin express-node route - How to populate object in express-MongoDB app
- How to render mongo object in Reactjs - How to render MongoDB object/ObjectId in React/MERN app
- How to render list of mongo objects in React component - How to render list of mongo objects in Reactjs
- How to place custom route links in Reactjs component - How to place custom links to routes/page in Reactjs
- How to filter MongoDB object list in express-node app - How to filter a mongo object list in document in express-node app
- How to fix mongo object Type error in Reactjs - How to fix TypeError/undefined in React - MERN application