【问题标题】:Joining tables in MongoDB with Mongoose in Nodejs Back-End将 MongoDB 中的表与 Nodejs 后端中的 Mongoose 连接起来
【发布时间】:2021-07-05 04:33:31
【问题描述】:

我是 MongoDB 的新学生,我有一个名为 usersSchema 的 MongoDB 架构。我需要将这个架构加入到incomeSchema。

这是我的 usersSchema:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcrypt');
const usersSchema = new Schema(
    {
        name: {
            type: String,
            required: true
        },
        pnumber: {
            type: String,
            required: true
        },
        email: {
            type: String,
            required: true,
            unique: true
        },
        password: {
            type: String,
            required: true
        }
    },
    { timestamps: true }
);
const Users = mongoose.model('Users', usersSchema);
module.exports = Users;

我需要将 usersSchema 电子邮件与我的 incomeSchema 加入。我不知道如何使用单个唯一数据加入这两个 Schema。

这是我的收入架构:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcrypt');
const incomeSchema= new Schema(
    {
        income: {
            type: double,
            required: true
        },
        email: { //This is the column I need to join and this colum come from usersSchema 
            type: String,
            required: true,
            unique: true
        },

    },
    { timestamps: true }
);
const Income= mongoose.model('Income ', incomeSchema);
module.exports = Income;

谢谢你..!

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema


    【解决方案1】:
    1. 您需要避免在 mongoDB 中加入(查找)
    2. 如果您仍需要加入,请检查聚合/$lookup

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-01
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      相关资源
      最近更新 更多