问题
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.
}
回答1:
Something like -> Index key:Name, Index key:City and not all.
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:
items:
[
{ Key: "Name", Value: "Peter", Type:String },
{ Key: "Age", Value: "18", Type:int },
{ Key: "City", Value: "San Jose", Type:String },
...30 to 40 items.
]
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" } )
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:
items:
{
"Name" : { Value: "Peter", Type:String },
"Age" : { Value: "18", Type:int },
"City" : { Value: "San Jose", Type:String },
...30 to 40 items.
}
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.
回答2:
Sorry, but mongoDB doesn't allow to index partly by condition.
来源:https://stackoverflow.com/questions/5078333/mongodb-c-sharp-array-index-or-indexing-inner-items-of-arrays