简体   繁体   English

MongoDB嵌入式文档作为键值数组

[英]MongoDB embedded document as key-val array

I'm trying to use mongoose to represent an association between 3 schemas: 我正在尝试使用猫鼬来表示3种架构之间的关联:

Schema1: {
    //fields
}

Schema2: {
    //fields    
}

Scehma3: {
   //fields
   - [Scehma1:Scehma2] - collection of key-val elements
     where the key is a ref to schema1 and val is ref to scehma2
}

Does mongoose support this king of association without creating a schema4? 猫鼬是否在不创建schema4的情况下支持这个联合之王?

You can't create ambiguous keys in mongoose because its whole purpose is to handle your document structure for you. 您不能在猫鼬中创建歧义键,因为它的全部目的是为您处理文档结构。 What you can do, however, is create an array of objects. 但是,您可以做的是创建对象数组。

Schema4: {
  schemaRefs: [{
    refToSchema1: {type: mongoose.Types.ObjectId, ref: 'Schema1'},
    refToSchema2: {type: mongoose.Types.ObjectId, ref: 'Schema2'}
  }]
}

For future reference it's far easier to understand your question when you provide real examples rather than false names. 为了将来提供参考,当您提供真实的示例而非虚假名称时,更容易理解您的问题。 Even if you falsify your example (eg some relationship between restaurants and customers or something) it's much easier to understand the relationship you're trying to make. 即使您伪造了示例(例如,餐馆与顾客之间的某种关系或某些事物),也更容易理解您试图建立的关系。

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

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