猫鼬模式中的嵌套对象

Posted

技术标签:

【中文标题】猫鼬模式中的嵌套对象【英文标题】:Nested objects in mongoose schemas 【发布时间】:2017-01-28 12:39:55 【问题描述】:

我在这里看到了很多关于这个问题的答案,但我仍然不明白(可能是因为他们使用了更“复杂”的例子)...... 所以我想要做的是一个“客户”的模式,它将有两个嵌套的“子字段”字段,以及其他可能重复的字段。这就是我的意思:

let customerModel = new Schema(
    firstName: String,
    lastName: String,
    company: String,
    contactInfo: 
        tel: [Number],
        email: [String],
        address: 
            city: String,
            street: String,
            houseNumber: String
        
       
);

telemail 可能是一个数组。 和地址不会重复,但有一些子字段,你可以看到。

我怎样才能做到这一点?

【问题讨论】:

【参考方案1】:
const mongoose = require("mongoose");

// Make connection
// https://mongoosejs.com/docs/connections.html#error-handling
mongoose.connect("mongodb://localhost:27017/test", 
  useNewUrlParser: true,
  useUnifiedTopology: true,
);

// Define schema
// https://mongoosejs.com/docs/models.html#compiling
const AddressSchema = mongoose.Schema(
  city: String,
  street: String,
  houseNumber: String,
);

const ContactInfoSchema = mongoose.Schema(
  tel: [Number],
  email: [String],
  address: 
    type: AddressSchema,
    required: true,
  ,
);

const CustomerSchema = mongoose.Schema(
  firstName: String,
  lastName: String,
  company: String,
  connectInfo: ContactInfoSchema,
);

const CustomerModel = mongoose.model("Customer", CustomerSchema);

// Create a record
// https://mongoosejs.com/docs/models.html#constructing-documents
const customer = new CustomerModel(
  firstName: "Ashish",
  lastName: "Suthar",
  company: "BitOrbits",
  connectInfo: 
    tel: [8154080079, 6354492692],
    email: ["asissuthar@gmail.com", "contact.bitorbits@gmail.com"],
  ,
);

// Insert customer object
// https://mongoosejs.com/docs/api.html#model_Model-save
customer.save((err, cust) => 
  if (err) return console.error(err);

  // This will print inserted record from database
  // console.log(cust);
);

// Display any data from CustomerModel
// https://mongoosejs.com/docs/api.html#model_Model.findOne
CustomerModel.findOne( firstName: "Ashish" , (err, cust) => 
  if (err) return console.error(err);

  // To print stored data
  console.log(cust.connectInfo.tel[0]); // output 8154080079
);

// Update inner record
// https://mongoosejs.com/docs/api.html#model_Model.update
CustomerModel.updateOne(
   firstName: "Ashish" ,
  
    $set: 
      "connectInfo.tel.0": 8154099999,
    ,
  
);

【讨论】:

我们如何分配一组服装类型,例如 [Address] 您仍然可以为每个嵌套字段设置默认值 我现在如何使用 required 的地址? @asissuthar 查看更新的答案以进行验证。 @Vinita【参考方案2】:
// address model
    var addressModelSchema = new Schema(
        city: String,
        street: String,
        houseNumber: String
    )
    mongoose.model('address',addressModelSchema ,'address' )

// contactInfo model
    var contactInfoModelSchema = new Schema(
        tel: [Number],
        email: [String],
        address: 
            type: mongoose.Schema.Type.ObjectId,
            ref: 'address'
        
    )
    mongoose.model('contactInfo ',contactInfoModelSchema ,'contactInfo ')

// customer model
    var customerModelSchema = new Schema(
        firstName: String,
        lastName: String,
        company: String,
        contactInfo: 
            type: mongoose.Schema.Type.ObjectId,
            ref: 'contactInfo'
          
    );
    mongoose.model('customer', customerModelSchema, 'customer')

// add new address then contact info then the customer info
// it is better to create model for each part.

【讨论】:

以上是关于猫鼬模式中的嵌套对象的主要内容,如果未能解决你的问题,请参考以下文章

猫鼬模式中的对象数组问题

用于多个对象数组的猫鼬嵌套模式

带有嵌套可选对象的猫鼬模式

带有嵌套可选对象和必填字段的猫鼬模式

更新猫鼬中的嵌套模式[重复]

猫鼬模式中的嵌套地图