繁体   English   中英

如何在 mongoose 模式中保存特定的 object

[英]How to save particular object in mongoose schema

我在这里遇到了一些问题,我尝试只存储一个 object 无论是学校还是大学还是工作,但是 mongoose 会自动创建另外两个 object。

如何省略其他object。这里我应该使用default

let mySchema = new Schema({
    "type": { type: String, default: "" },  // school or college or work
    "school": {
        'name':{ type: String, default: "" },
        'parent':{ type: String, default: "" },
        'address':{ type: String, default: "" },
        'email_id':{ type: String, default: "" },
        'phone_no':{ type: String, default: "" },
    },
    "college": {
        'name':{ type: String, default: "" },
        'friend':{ type: String, default: "" },
        'address':{ type: String, default: "" },
        'email_id':{ type: String, default: "" },
        'phone_no':{ type: String, default: "" },
    },
    "work": {
        'name':{ type: String, default: "" },
        'colleague':{ type: String, default: "" },
        'address':{ type: String, default: "" },
        'email_id':{ type: String, default: "" },
        'phone_no':{ type: String, default: "" },
    },
});

是否有什么东西阻止你以编程方式设置它?

 const myType = "school"; // Just for example const mySchemaObj = { "type": { type: String, default: myType } }; mySchemaObj[myType] = { "school": { 'name':{ type: String, default: "" }, 'parent':{ type: String, default: "" }, 'address':{ type: String, default: "" }, 'email_id':{ type: String, default: "" }, 'phone_no':{ type: String, default: "" }, }, "college": { 'name':{ type: String, default: "" }, 'friend':{ type: String, default: "" }, 'address':{ type: String, default: "" }, 'email_id':{ type: String, default: "" }, 'phone_no':{ type: String, default: "" }, }, "work": { 'name':{ type: String, default: "" }, 'colleague':{ type: String, default: "" }, 'address':{ type: String, default: "" }, 'email_id':{ type: String, default: "" }, 'phone_no':{ type: String, default: "" }, } }[myType]; console.log(mySchemaObj); /* Then, later... let mySchema = new Schema(mySchemaObj); */

如果您从 Mongoose 文档中查看部分,您将看到 Mongoose 总是为嵌套文档(例如school )创建一个空的 object。

您可以改用子文档,其中schoolcollegeworkSchema的实例。

你可以做什么:

const mySchema = new Schema({
  school: new Schema({
    name: { type: String },
    parent: { type: String },
    address: { type: String },
    email_id: { type: String },
    phone_no: { type: String },
  }),
  college: new Schema({
    name: { type: String },
    friend: { type: String },
    address: { type: String },
    email_id: { type: String },
    phone_no: { type: String },
  }),
  work: new Schema({
    name: { type: String },
    colleague: { type: String },
    address: { type: String },
    email_id: { type: String },
    phone_no: { type: String },
  }),
});
const MyModel = mongoose.model('MyModel', mySchema);
const myDocument = new MyModel();
myDocument.college = { name: 'Harvard' };
await myDocument.save();

暂无
暂无

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

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