MongoDB Table Design and Query Performance

荒凉一梦 提交于 2019-12-06 07:28:09

A document could be viewed like a table with columns, but you have to be carefull. It has other usage characteristics. The document size can be max. 16 MB. And you have to keep in mind that the documents are hold in memory by mongo.

With your query the whole document will be returned. Ask yourself do you need all entries or will you have to use a single entry on his own?

This should be a good start.

What is data? I wouldn't store a single nested document with up to 100,000 fields as it you wouldn't be able to index it easily so you would get performance issues.

You'd be better off storing as an array of strings, then you can index the array field which would index all the values.

{
    "name" : string,
    "data" : [ "xxx", "yyy", "zzz" ]
}

If like in your query you then wanted the value at a particular position in the array, instead of data.data3 you could do:

db.Collection.find( { "data.2" : "zzz" } )

Or, if you don't care about the position and just want all documents where the data array contains 'zzz' you can do:

db.Collection.find( { "data" : "zzz" } )

100,000 strings is not going to get anywhere near 16MB so you don't need to worry about that, but having 100,000 fields in a nested document or array indicates something is wrong with the design, but without knowing what data is I couldn't say for sure.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!