0

I am learning mongodb and trying it out with a given requirement as follows:

data is formatted in a flat file as:

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

the file names vary both in names and counts. attributes per file name are fixed. There are multiple users.

I need to be able to query based on attribute values.

I found a relevant question here: How to get a specific embedded document inside a MongoDB collection?

wondering whats the best schema is for my requirement.

Or is mongodb a bad choice for this?

Community
  • 1
  • 1
powerrox
  • 1,334
  • 11
  • 21

1 Answers1

1

There is no schema in mongodb, you can change the way the document stored as you wish, You can try to this per user, where the filenames is an array of that can vary in size, and since the attributes are fixed in size, then you can specify the fixed set of attributes where you can query for files[X].attribute value.

{
  "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"
            }
          ]
}
faljbour
  • 1,367
  • 2
  • 8
  • 15
  • I came back to add a comment that the solution from the other thread I linked worked. You responded with the same solution. I just finished implementing it and yes, it works very well. Sorry about the terminology (schema) – powerrox Apr 13 '15 at 05:06