【问题标题】:design mongodb schema with variable fields设计带有可变字段的mongodb模式
【发布时间】:2015-06-18 06:41:14
【问题描述】:

我正在学习 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 schema


    【解决方案1】:

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

    {
      "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"
                }
              ]
    }
    

    【讨论】:

    • 我回来添加评论说我链接的另一个线程的解决方案有效。您用相同的解决方案做出了回应。我刚刚完成了它,是的,它工作得很好。对术语(架构)感到抱歉
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 2017-04-17
    • 2015-01-26
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多