0

I am new for node.js and mongodb created api using help of google

Created Api using monogdb + nodejs.

Working fine with single table CURD operation

But don't know how to join in node.js

request.model.js

const mongoose = require('mongoose');

const RequestSchema = mongoose.Schema({
    userId: String,
    title: String,
    description: String
}, {
    timestamps: true
});

module.exports = mongoose.model('Request', RequestSchema);

user.model.js

const mongoose = require('mongoose');

const UserSchema = mongoose.Schema({
    firstname: String,
    lastname: String
}, {
    timestamps: true
});

module.exports = mongoose.model('User', UserSchema);

user.controller.js

const User = require('../models/user.model.js');

// Retrieve and return all notes from the database.
exports.findAll = (req, res) => {
  jwt.verify(req.token, 'secretkey', (err, authData) => {
    if(err){
      res.sendStatus(403);
    }else{
      User.find()
      .then(user => {
        res.send(user);
      }).catch(err => {
        res.status(500).send({
          message: err.message || "Some error occurred while retrieving user."
        });
      });
    }
  })
};

request.controller.js

const Request = require('../models/request.model.js');

// Retrieve and return all notes from the database.
exports.findAll = (req, res) => {
  jwt.verify(req.token, 'secretkey', (err, authData) => {
    if(err){
      res.sendStatus(403);
    }else{
      Request.find()
      .then(request => {
        res.send(request);
      }).catch(err => {
        res.status(500).send({
          message: err.message || "Some error occurred while retrieving request."
        });
      });
    }
  })
};

request table data structure

{
    "_id": {
        "$oid": "5e3bef0be7b75760bf31d4c8"
    },
    "userId": "5e3bdf5919c9d8586d7f2455",
    "title": "test title",
    "description": "test desc",
    "createdAt": {
        "$date": {
            "$numberLong": "1580986123184"
        }
    },
    "updatedAt": {
        "$date": {
            "$numberLong": "1580986123184"
        }
    },
    "__v": {
        "$numberInt": "0"
    }
}

user table data structure

{
    "_id": {
        "$oid": "5e3bdf5919c9d8586d7f2455"
    },
    "firstname": "bhavik",
    "lastname": "gajjar",
    "createdAt": {
        "$date": {
            "$numberLong": "1580982105830"
        }
    },
    "updatedAt": {
        "$date": {
            "$numberLong": "1580982105830"
        }
    },
    "__v": {
        "$numberInt": "0"
    }
}

Thanks in advance

1
  • Please tell response data Commented Feb 6, 2020 at 11:33

1 Answer 1

2

If you want to query for Requests with joined users, do this:

Request.aggregate([
   {
     $lookup: {
        from: "Users", // collection name in db
        localField: "userId",
        foreignField: "_id",
        as: "user"
     }
   }
])
Sign up to request clarification or add additional context in comments.

1 Comment

Just replace this fragment Request.find() in request.controller.js with my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.