适当地在 mongoDb 中创建数据
Create Data in mongoDb appropriately
我是 node.js 的新手,正在尝试创建获取服务。涵盖了节点部分,但由于这是我第一次做后端,所以我想在建模数据方面获得一些建议。
我有一个非常简单的 JSON,我想在我的 MongoDB 中使用它。
{
"names": [
“xyz",
“abc",
“def",
“wxy",
“stu",
“ghi"
],
“xyz": {
“name": “XYZ",
“at": “xyzid",
“bird": “xyzbird",
"music": “xyzmusic",
“tube": “xyztube",
"featured”:true
},
“abc": {
“name": “ABC",
“at": “abcat",
“bird": “abcbird",
“music": “abcmusic",
"tube": “abctube",
"featured":false
},
“def": {
“name": “DEF",
“at": “DEFAT",
“bird": “DEFbird",
“music": “defmusic",
"tube": “deftube",
"featured":false
},
“wxy": {
“name": “WXY",
“at": “wxyat",
“bird": “wxybird",
“music": “wxymusic",
"tube": “wxytube",
"featured":false
},
“stu": {
“name": “STU",
“at": “stuat",
“bird": “stubird",
“music": “stumusic",
"tube": “stutube",
"featured":false
},
“ghi": {
“name": “GHI",
“at": “ghiat",
“bird": “ghibird",
“music": “ghimusic",
"tube": “ghitube",
"featured":false
}
}
到目前为止,通过在线阅读,我似乎应该创建 2 个不同的集合,一个是 'Names',另一个是 'Details'。这是正确的吗 ?
是否有其他最佳方式将此数据存储在 MongoDb 中,并且无需进行过多处理即可轻松访问这些数据。我正在使用 Mongoose 与 mongodb.
进行交互
提前致谢,如果内容过于宽泛,我们深表歉意。
当然有。但首先你应该决定你的名字数组中会有多少名字。如果名字很少,那么你可以这样做:
{
"names": [
{
name: “xyz",
details: {
“name": “XYZ",
“at": “xyzid",
“bird": “xyzbird",
"music": “xyzmusic",
“tube": “xyztube",
"featured”:true
}
},
{
name: “abc",
details: {
“name": “ABC",
“at": “abcat",
“bird": “abcbird",
“music": “abcmusic",
"tube": “abctube",
"featured":false
}
},
{
name: “def",
details: {
“name": “DEF",
“at": “DEFAT",
“bird": “DEFbird",
“music": “defmusic",
"tube": “deftube",
"featured":false
}
},
{
name: “wxy",
details: {
“name": “WXY",
“at": “wxyat",
“bird": “wxybird",
“music": “wxymusic",
"tube": “wxytube",
"featured":false
}
},
{
name: “stu",
details: {
“name": “STU",
“at": “stuat",
“bird": “stubird",
“music": “stumusic",
"tube": “stutube",
"featured":false
}
},
{
name: “ghi",
details: {
“name": “GHI",
“at": “ghiat",
“bird": “ghibird",
“music": “ghimusic",
"tube": “ghitube",
"featured":false
}
}
]}
但是如果名称数组中有数百万个名称,那么我建议您为每个文档创建一个 names
集合,例如:
{
“name": “GHI",
“at": “ghiat",
“bird": “ghibird",
“music": “ghimusic",
"tube": “ghitube",
"featured":false
}
我也认为在这种情况下不需要名称字段。
我是 node.js 的新手,正在尝试创建获取服务。涵盖了节点部分,但由于这是我第一次做后端,所以我想在建模数据方面获得一些建议。
我有一个非常简单的 JSON,我想在我的 MongoDB 中使用它。
{
"names": [
“xyz",
“abc",
“def",
“wxy",
“stu",
“ghi"
],
“xyz": {
“name": “XYZ",
“at": “xyzid",
“bird": “xyzbird",
"music": “xyzmusic",
“tube": “xyztube",
"featured”:true
},
“abc": {
“name": “ABC",
“at": “abcat",
“bird": “abcbird",
“music": “abcmusic",
"tube": “abctube",
"featured":false
},
“def": {
“name": “DEF",
“at": “DEFAT",
“bird": “DEFbird",
“music": “defmusic",
"tube": “deftube",
"featured":false
},
“wxy": {
“name": “WXY",
“at": “wxyat",
“bird": “wxybird",
“music": “wxymusic",
"tube": “wxytube",
"featured":false
},
“stu": {
“name": “STU",
“at": “stuat",
“bird": “stubird",
“music": “stumusic",
"tube": “stutube",
"featured":false
},
“ghi": {
“name": “GHI",
“at": “ghiat",
“bird": “ghibird",
“music": “ghimusic",
"tube": “ghitube",
"featured":false
}
}
到目前为止,通过在线阅读,我似乎应该创建 2 个不同的集合,一个是 'Names',另一个是 'Details'。这是正确的吗 ?
是否有其他最佳方式将此数据存储在 MongoDb 中,并且无需进行过多处理即可轻松访问这些数据。我正在使用 Mongoose 与 mongodb.
进行交互提前致谢,如果内容过于宽泛,我们深表歉意。
当然有。但首先你应该决定你的名字数组中会有多少名字。如果名字很少,那么你可以这样做:
{
"names": [
{
name: “xyz",
details: {
“name": “XYZ",
“at": “xyzid",
“bird": “xyzbird",
"music": “xyzmusic",
“tube": “xyztube",
"featured”:true
}
},
{
name: “abc",
details: {
“name": “ABC",
“at": “abcat",
“bird": “abcbird",
“music": “abcmusic",
"tube": “abctube",
"featured":false
}
},
{
name: “def",
details: {
“name": “DEF",
“at": “DEFAT",
“bird": “DEFbird",
“music": “defmusic",
"tube": “deftube",
"featured":false
}
},
{
name: “wxy",
details: {
“name": “WXY",
“at": “wxyat",
“bird": “wxybird",
“music": “wxymusic",
"tube": “wxytube",
"featured":false
}
},
{
name: “stu",
details: {
“name": “STU",
“at": “stuat",
“bird": “stubird",
“music": “stumusic",
"tube": “stutube",
"featured":false
}
},
{
name: “ghi",
details: {
“name": “GHI",
“at": “ghiat",
“bird": “ghibird",
“music": “ghimusic",
"tube": “ghitube",
"featured":false
}
}
]}
但是如果名称数组中有数百万个名称,那么我建议您为每个文档创建一个 names
集合,例如:
{
“name": “GHI",
“at": “ghiat",
“bird": “ghibird",
“music": “ghimusic",
"tube": “ghitube",
"featured":false
}
我也认为在这种情况下不需要名称字段。