Mongo with mongoose

How to display MongoDB Object using the Mongoose in MongoDB-Express app

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.

The mongoose schema

const mongoose = require('mongoose');
 const PostSchema = new mongoose.Schema({
     title: {
         type: String,
         required:   '{PATH} is required!'
     content: {
         type: String,
         required:   '{PATH} is required!'
  user: {     type: mongoose.Schema.Types.ObjectId,     ref: 'User' }
 }, {
     timestamps: true
 module.exports = mongoose.model('Post', PostSchema);

Here the user field is an example for the Object ID field and the resulted document will look like the following

New Post:{                                                                                                                      
   _id: 5fee99c33cbe8b25d0ced080,                                                                                                
   title: 'Happy New Year',                                                                                                      
   content: 'Happy New Year MEssage',                                                                                            
   user: 5fe4ab1e69b8d525c44ec293,                                                                                               
   createdAt: 2021-01-01T03:40:51.340Z,                                                                                          
   updatedAt: 2021-01-01T03:40:51.340Z,                                                                                          
   __v: 0                                                                                                                        

Populate the user

Using the populate command we can get the complete User information which will be show as sub document/sub object. To populate the result we have to use the populate ,exec and create a caller function

Post.findById(post._id).populate('user').exec(function(error, post) {
                 console.log('New post - > ' + post);

and the resulted document will be

New post - > {         
        "_id": "5fe6ca78443a4d0f54aafecf",
        "title": "Hosting",
        "content": "4 Month free hosting plans for 100 developer opens now",
        "user": {   
            "_id": "5fe4db85a4896d069006601b",
            "name": "emma",
            "createdAt": "2020-12-24T18:18:45.224Z",
            "updatedAt": "2020-12-24T18:20:47.857Z",
            "__v": 1
        "createdAt": "2020-12-26T05:30:32.715Z",
        "updatedAt": "2020-12-26T05:30:32.715Z",
        "__v": 0

That’s it. Following mongoose posts deserve your attention

Published by


A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.