MongoDB C# 数组索引或索引数组的内部项
我在dynamicArray中使用动态数组存储键、值、类型。 MongoDB/C# 通常使用数组的索引,如 db.contents.ensureIndex ( {dynamicArray : 1 } )。使用此方法存储超过 30 或 40 个元素会生成大量信息以进行索引。存在另一种方法来索引不是完整的数组,而是该数组的项目键,限制了索引存储。类似 -> 的东西索引键:名称,索引键:城市而不是全部。
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.
}
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.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能专门执行此操作,不能对键的值建立索引。
一个解决方案
但是,您可以对数组中的项目建立索引。
假设您的数据如下所示:
您将执行以下操作来在
items.Key
上创建索引:当您执行以下操作时,您将使用该索引:
这会将搜索范围缩小到仅这些项目具有
Key = "City"
。如果这就是一切,那么这可能没有帮助。替代解决方案
为什么
items
是一个数组?您能不能像这样构建数据:现在您可以在
items.City.Value
上建立索引,这就是您首先要寻找的内容。这也使得数据结构变得更小。根据数据的性质,您可能还需要查看稀疏索引 帮助控制索引的大小。
You cannot do this specifically, you cannot index on the value of key.
One Solution
However, you can index on items in an array.
Let's assume that your data looks like this:
You would do the following to create an index on
items.Key
:When you do the following you will use the index:
This will narrow the search to only those items that have
Key = "City"
. If this is everything, then this probably won't help.Alternate solution
Why is
items
an array? Can you not structure data like this: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.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 不允许按条件部分索引。
Sorry, but mongoDB doesn't allow to index partly by condition.