简体   繁体   English

如何使用 MongoDB + Node.js API 连接两个表?

[英]How to Join Two table using MongoDB + Node.js API?

I am new for node.js and mongodb created api using help of google我是 node.js 的新手,mongodb 使用谷歌的帮助创建了 api

Created Api using monogdb + nodejs.使用 monogdb + nodejs 创建了 Api。

Working fine with single table CURD operation使用单表 CURD 操作正常工作

But don't know how to join in node.js但是不知道怎么加入node.js

request.model.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用户模型.js

const mongoose = require('mongoose');

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

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

user.controller.js用户控制器.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 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提前致谢

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"
     }
   }
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM