簡體   English   中英

如何在Mongoose中將架構屬性設置為SubDocument類型?

[英]How do you set a schema property to be of type SubDocument in Mongoose?

我想做這樣的事情:

var userSchema = new Schema({
  local: localSchema,
  facebook: facebookSchema,
  twitter: twitterSchema,
  google: googleSchema
});

但是似乎Schema不是有效的SchemaType

在SubDocuments 指南中 ,他們僅提供一個示例,其中將子架構放在數組中,但這不是我想要的。

var childSchema = new Schema({ name: 'string' });

var parentSchema = new Schema({
  children: [childSchema]
})

看來您只是想為每個屬性創建一個子對象。 您可以通過以下兩種方法之一來完成此操作。

嵌入模式本身

var userSchema = new Schema({
    local: {
        someProperty: {type: String}
        //More sub-properties...
    }
    //More root level properties
});

可在多個架構中使用的可重用對象

//this could be defined in a separate module and exported for reuse
var localObject = {
    someProperty: {type: String}
    //more properties
}

var userSchema = new Schema({
    local: localObject
});

var someOtherSchema = new Schema({
    test: localObject
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM