gpt4 book ai didi

javascript - 省略 mongodb 中不必要的属性

转载 作者:行者123 更新时间:2023-12-03 01:31:09 25 4
gpt4 key购买 nike

我有这个模型:

var accountSchema = new mongoose.Schema({
'seeker': {
'fullName': String,
'ageGroup': String,
'education': String,
'lookingForWork': String,
'employmentStatus': String,
'resume': String, // Filename ans stuff
'mainWorkExp': String,
},
'employer': {
'activationStatus': String,
'companyName': String,
'contactPersonFullName': String,
'companyWebsite': String,
'hiringRegion': [String],
'logo': [],
'description': String,
'industry': String,
'workingWithEOSP': Boolean,
'booth': {
'exists': Boolean, // Boolean to store with the employer created his/her booth or not
'numberVisits': Number // number of clicks on the particular booth
/* We can add more attributes here as we go */
}
},
});

它已导出,一切都很棒。但有一个问题。如果我尝试使用此脚本创建一个搜索者帐户:

new account({
'seeker': {
'fullName': r.fullName,
'ageGroup': r.ageGroup,
'education': r.education,
'lookingForWork': r.lookingForWork,
'employmentStatus': r.employmentStatus,
'mainWorkExp': r.mainWorkExp,
'resume': r.file,
},
}),
r.password,

实际上我在数据库中得到了这个:

{
"_id": {
"$oid": "5b4689ab6d68e81d32fda65e"
},
"seeker": {
"fullName": "Alex Ironside",
"ageGroup": "18 - 24",
"education": "Some High School",
"lookingForWork": "Less than 1 month",
"employmentStatus": "Unemployed",
"mainWorkExp": "Business, Finance or Administration. Includes all levels of office admin, accounting/bookkeeping, human resources, banking, etc."
},
"employer": { // The problem is right here!
"hiringRegion": [],
"logo": []
}
}

正如您所看到的,我没有在代码中的任何位置提及 logohiringRegion,但它仍然会被创建。这是为什么?这是否与它们在模型中都被声明为数组有关?我该如何解决这个问题?

我基本上不希望在我的代码创建的文档中创建employer属性/对象。它应该只是 seeker 对象,但我不能只从模型中删除 logohiringRegions,因为我在其他地方使用它们,作为数组。

r 对象是 req.body 的缩写。

最佳答案

Mongoose 数组 implicitly have a default value of empty array这样您就可以立即开始使用该阵列。改为这样做:

'hiringRegion': { type: [String], default: undefined },
'logo': { type: [], default: undefined }

关于javascript - 省略 mongodb 中不必要的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51295289/

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