设计 mongodb 具有可变字段的模式

design mongodb schema with variable fields

我正在学习 mongodb 并按照给定的要求进行尝试,如下所示:

数据在平面文件中的格式为:

id: number
user: string
filename1: string
filename1:id: number
filename1:attribute1: number
filename1:attribute2: number
filename1:attribute3: number

filename2: string
filename2:id: number
filename2:attribute1: number
filename2:attribute2: number
filename2:attribute3: number

...

filename_n: string
filename_n:id: number
filename_n:attribute1: number
filename_n:attribute2: number
filename_n:attribute3: number

文件名在名称和数量上都不同。每个文件名的属性是固定的。有多个用户。

我需要能够根据属性值进行查询。

我在这里找到了一个相关问题: How to get a specific embedded document inside a MongoDB collection?

想知道什么是最适合我的要求的模式。

或者 mongodb 是一个糟糕的选择吗?

mongodb 中没有模式,您可以根据需要更改文档的存储方式,您可以针对每个用户尝试此操作,其中文件名是一个大小可以变化的数组,并且由于属性的大小是固定的,因此您可以指定固定的属性集,您可以在其中查询文件[X].属性值。

{
  "id": "44",
  "user":"Jack Smith",
  "files":[
            {
              "name":"filename1",
              "id":"21",
              "attribute1":  "4444",
              "attribute2":  "5555",
              "attribute3":  "7777",
              "attribute4":  "8888"
            },
            {
              "name":"filename2",
              "id":"55",
              "attribute1":  "1111",
              "attribute2":  "3333",
              "attribute3":  "9999",
              "attribute4":  "80000"
            },
            {
              "name":"filename3",
              "id":"33",
              "attribute1":  "4444",
              "attribute2":  "5555",
              "attribute3":  "7777",
              "attribute4":  "8888"
            },
          ..............
            {
              "name":"filenamen",
              "id":"45",
              "attribute1":  "4444",
              "attribute2":  "5555",
              "attribute3":  "7777",
              "attribute4":  "8888"
            }
          ]
}