gpt4 book ai didi

node.js - 我的 Mongoose 关系出了什么问题?

转载 作者:太空宇宙 更新时间:2023-11-04 01:27:25 27 4
gpt4 key购买 nike

我正在学习 Mongoose 关系并陷入这个问题......有人告诉我我错在哪里......?有没有更好的方法来做到这一点..?

Employee.js

const mongoose = require("mongoose");
const { departmentSchema, addDepartment } = require("./Departments");

const schema = new mongoose.Schema();

const employeeModel = mongoose.model("Employee", {
Name: { type: String, required: true, minlength: 5, maxlength: 50 },
Department: departmentSchema
});

async function addEmployee(Employee, Department, Language, Framework) {
const result = await new employeeModel({
...Employee,
Department: addDepartment(Department, Language, Framework)
});
return await result.save();
}

Department.js

const mongoose = require("mongoose");
const { languageSchema, addLanguage } = require("./Language");

const departmentSchema = new mongoose.Schema({
Depart: { type: String, required: true, minlength: 5, maxlength: 50 },
Languages: languageSchema
});

const DepartmentModel = mongoose.model("Department", departmentSchema);

async function addDepartment(Department, Language, Framework) {
const result = await new DepartmentModel({
...Department,
Languages: addLanguage(Language, Framework)
});
return await result.save();
}

module.exports = {
departmentSchema,
addDepartment
};

语言.js

const mongoose = require("mongoose");
const { frameworkSchema, addFramework } = require("./Frameworks");

const languageSchema = new mongoose.Schema({
Language: { type: String, required: true },
Framework: [frameworkSchema]
});

const languageModel = mongoose.model("Language", languageSchema);

async function addLanguage(Language, Framework) {
const result = await new languageModel({
...Language,
Framework: addFramework(Framework)
});
return await result.save();
}

module.exports = {
languageSchema: languageSchema,
addLanguage
};

Framework.js

const mongoose = require("mongoose");

const frameworkSchema = new mongoose.Schema({
Name: { type: String, required: true, minlength: 4, maxlength: 50 },
Version: { type: String, required: true, minlength: 1, maxlength: 50 }
});

const frameworkModel = mongoose.model("Framework", frameworkSchema);

async function addFramework(Framework) {
const result = await new frameworkModel(...Framework);
return await result.save();
}

module.exports = {
frameworkSchema,
addFramework
};

Framework.js 错误

Found non-callable @@iterator

我想做的是

  1. 员工 ->
  2. 姓名
  3. 部门 ->
  4. 出发
  5. 语言 ->
  6. 语言名称
  7. 框架 ->
  8. 框架名称
  9. 框架版本

每个 -> 代表一个新对象(集合)

我知道对于像我这样的新手来说这有点复杂......我是 MySql 数据库用户,我喜欢它,但现在我正在尝试掌握 NoSql...

提前致谢:)

最佳答案

我将向您展示一个示例:假设您已经有 employSchema 了:

const {Schema} = mongoose;
const employeeModel = mongoose.model("Employee", new Schema({
Name: { type: String, required: true, minlength: 5, maxlength: 50 },
Department: {type: Schema.Types.ObjectId, ref: "Department"}
}));

在上面的模型中,我使用 ref 属性通过 ObjectId 连接到 Department 模型(这意味着 Department 集合中的“_id”属性)。

例如:您将创建一个新的employeeModel,如下所示:

let newEmployee = new employeeModel("Employee Name", Department ID here).

你绝对会做相反的事或为别人做事。并且需要使用.populate()函数来查询数据库。示例:

employModel.find({}).populate("Department").exec(callback what you want to do);

您可以阅读官方文档:https://mongoosejs.com/docs/populate.html

关于node.js - 我的 Mongoose 关系出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57041090/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com