2017-04-10 46 views
0

我有一個很大的MongoDb集合,用戶可以在其中搜索多個Array字段。如何索引這個最好的方法?我看到創建多陣列索引索引與結束:在多個陣列字段上提高MongoDB性能

「蒙戈::錯誤:: OperationFailure:不能索引平行陣列」

實施例的查詢:

db.collection.find(
    {category_ids: {$in: [object_id, object_id]}, 
    second_category_ids: $in: {[object_id, object_id]} 
    } 
) 

回答

0

作爲每MongoDB的documentation,並行數組上的多鍵索引是不允許的。下面是什麼文件讀取 -

As such, you cannot create a compound multikey index if more than one to-be-indexed field of a document is an array. Or, if a compound multikey index already exists, you cannot insert a document that would violate this restriction.

最大我們可以做的就是分別索引兩個字段和查詢將使用它感覺最好的索引。如果你比查詢規劃器有更好的想法,那麼提示也可以用來使用特定的索引。

或者你可以考慮重構你的模式。例如,子類別可以在不同的集合中進行。如果我更瞭解數據模式和模式設計,可以幫助我。

+0

我有一個類似__approved__,__banned__,__reviewed__等__Event__模型。這些總是被質疑。但也可以搜索__country__ __region__ __city__等字段。這些取決於用戶的行爲。最後但並非最不重要的是,用戶能夠搜索該事件的__category_ids__和__musicstyle_ids__中的任一個或兩個,這是陣列。 – Jan

+0

我做了一些谷歌,發現這個http://stackoverflow.com/questions/25059525/indexing-parallel-arrays-in-mongodb#comment39032157_25067084 –