如何在mongoose模式中正确声明数组?

如何在mongoose模式中正确声明数组?,mongoose,Mongoose,我想知道以下在mongoose模式中声明数组的方法之间是否有区别。一个对吗?他们有什么不同吗 authors: [{ type: String }] vs 谢谢。好吧,两者都有效。但是如果你想知道两者的区别,那么让我来告诉你它到底有什么区别 案例1: authors: [{ type: String }] 当您在模式中像上面这样编写时,这意味着authors键将包含字符串数组。更准确地说,文档的创建方式如下: authors:[ "Adam Freeman,

我想知道以下在mongoose模式中声明数组的方法之间是否有区别。一个对吗?他们有什么不同吗

authors: [{
    type: String
}]
vs


谢谢。

好吧,两者都有效。但是如果你想知道两者的区别,那么让我来告诉你它到底有什么区别

案例1:

authors: [{
    type: String
}]
当您在模式中像上面这样编写时,这意味着authors键将包含字符串数组。更准确地说,文档的创建方式如下:

authors:[
    "Adam Freeman,
    "Kristina Chodorow",
    "Kyle Banker",
    "Eelco Plugge"
]
authors:[
    {
        author:"Adam Freeman"
    },
    {
        author:"Kristina Chodorow"
    },
    {
        author:"Kyle Banker"
    },
    {
       author:" Eelco Plugge"
    }
]
案例2:

authors: [{
    author: {
        type: String
    }
}]
当您在模式中像上面那样编写时,这意味着authors可能包含多个键为“author”的对象,并且类型为string。更准确地说,文档的创建方式如下:

authors:[
    "Adam Freeman,
    "Kristina Chodorow",
    "Kyle Banker",
    "Eelco Plugge"
]
authors:[
    {
        author:"Adam Freeman"
    },
    {
        author:"Kristina Chodorow"
    },
    {
        author:"Kyle Banker"
    },
    {
       author:" Eelco Plugge"
    }
]
所以,现在这取决于您希望如何设计数据库