node.js - 将 MongoDB 中的表与 Nodejs 后端中的 Mongoose 连接起来
问题描述
我是 MongoDB 的新学生,我有一个名为 usersSchema 的 MongoDB Schema。我需要将这个 Schema 加入到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;
谢谢你..!
解决方案
- 您需要避免在 mongoDB 中加入(查找)
- 如果您仍需要加入,请检查聚合/$lookup
推荐阅读
- javafx - IDEA下的JavaFX WebView
- python - 将字典键映射到值的问题
- amazon-web-services - 将 aws cli 切换到启用 mfa 的跨帐户
- r - R中的UTF-8编码
- angularjs - 如何等待http响应赋值?
- azure - Azure Cosmos DB - 创建容器时出现“RequestDisallowedByPolicy”错误
- python - 如何打印系列中第 75 个百分位以上的值?使用分位数(熊猫)
- solr - solr 查询“msg”:“未定义的字段文本”,“代码”:400
- python - 如何使用 python 使光标在游戏中移动(例如:第一枪射击游戏)
- npm - npm 全局安装正在安装,但命令不起作用