2017-04-10 42 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 –