MongoDB C# 数组索引或索引数组的内部项 [英] MongoDB C# Array index or indexing inner items of arrays
问题描述
我使用动态数组存储键、值、类型为 dynamicArray.MongoDB/C# 通常使用数组的索引,如 db.contents.ensureIndex ( { dynamicArray : 1 } ).存储超过 30 或 40 个元素会生成大量信息以使用此方法进行索引.存在另一种方法来索引不是完整数组而是限制索引存储的该数组的项键.类似于 -> 索引键:名称,索引键:城市而不是全部.
I use a dynamic array storage key, value, type in dynamicArray. MongoDB/C# generaly use an index of the array like db.contents.ensureIndex ( { dynamicArray : 1 } ). Storing more than 30 or 40 elements are generating a big information to index using this method. Exist another way to index not the complete array but items key of this array limiting the index storage. Something like -> Index key:Name, Index key:City and not all.
dynamicArray:
{
item : { Key: "Name", Value: "Peter", Type:String }
item : { Key: "Age", Value: "18", Type:int }
item : { Key: "City", Value: "San Jose", Type:String }
...30 to 40 items.
}
推荐答案
类似 -> 索引键:名称,索引键:城市而不是全部.
Something like -> Index key:Name, Index key:City and not all.
你不能专门做这个,你不能索引键的值.
You cannot do this specifically, you cannot index on the value of key.
一个解决方案
但是,您可以对数组中的项目进行索引.
However, you can index on items in an array.
假设您的数据如下所示:
Let's assume that your data looks like this:
items:
[
{ Key: "Name", Value: "Peter", Type:String },
{ Key: "Age", Value: "18", Type:int },
{ Key: "City", Value: "San Jose", Type:String },
...30 to 40 items.
]
您将执行以下操作以在 items.Key
上创建索引:
You would do the following to create an index on items.Key
:
db.foo.ensureIndex( { 'items.Key' } )
当您执行以下操作时,您将使用索引:
When you do the following you will use the index:
db.foo.find( { 'items.Key' : "City", 'items.value' : "San Jose" } )
这会将搜索范围缩小到仅具有 Key = "City"
的项目.如果这就是一切,那么这可能无济于事.
This will narrow the search to only those items that have Key = "City"
. If this is everything, then this probably won't help.
替代解决方案
为什么 items
是一个数组?你能不能这样构造数据:
Why is items
an array? Can you not structure data like this:
items:
{
"Name" : { Value: "Peter", Type:String },
"Age" : { Value: "18", Type:int },
"City" : { Value: "San Jose", Type:String },
...30 to 40 items.
}
现在您可以对 items.City.Value
建立索引,这正是您首先要寻找的.这也使得数据结构小了很多.
Now you can index on items.City.Value
, which is what you were looking for in the first place. This also makes the data structure quite a bit smaller.
根据数据的性质,您可能还想查看 sparse索引以帮助控制索引的大小.
Depending on the nature of your data, you may also want to look at sparse indexes to help control the size of your index.
这篇关于MongoDB C# 数组索引或索引数组的内部项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!