I have a collection called Post. I have a mapping system that always ensures that each document has these fields:
- id (int)
- target (string)
- type (string)
- user_id
- client_id
- updated (string, 11 int timestamp)
- created (string, 11 int timestamp)
- enabled (bool)
This collection is accessed to be output in an API schema.
So some typical requests might be:
/post?type=image&user_id=2
/post?updated=35234423&order_by=client_id
/post?enabled=true&order_by=id
There is no 100% guarantee certain fields make it to the find or sort field.
Recently when the table reached 8GB of data, I started getting this error:
"localhost:27017: too much data for sort() with no index. add an index or specify a smaller limit"
I have looked at the documentation for Mongo index and found it difficult to understand whether it works in the same way as a MySQL index.
Some threads I found on indexing: MongoDB - too much data for sort() with no index error seem to suggest using specific sort fields to ensure the index is hit. Obviously I cannot do that when alot of my filtering and sorting is optional.
Could anyone suggest what a firm solution would be in terms of whether I should index all fields on my table?
Thanks for the feedback guys, I've started building an auto index function:
public function get() {
$indices['Post'] = array(
'fields' =>
array(
'id' => array('unique' => true, 'dropDups' => true, 'background' => true),
'client_id' => array('dropDups' => true, 'background' => true),
'image_id' => array('dropDups' => true, 'background' => true),
'user_id' => array('dropDups' => true, 'background' => true),
'publish_target' => array('dropDups' => true, 'background' => true),
'type' => array('dropDups' => true, 'background' => true),
'status' => array('dropDups' => true, 'background' => true),
'text' => array('background' => true)
)
);
foreach ($indices as $key => $index) {
/* set the collection */
$collection = $this->mongoDB->{$key};
/* delete the indexes */
$collection->deleteIndexes();
/* loop the fields and add the index */
foreach ($index['fields'] as $subKey => $data) {
$collection->ensureIndex($subKey, array_merge($data, array('name' => $subKey)));
}
}
/* return the list */
return $indices;
}