你可以存储你的数据进行动态查询为“输入”键 - 值对的数组,像这样:在.TYPE
query_data: [
{ type: "company", value: "foo" },
{ type: "plan", value: "pro" },
...
]
,然后指数和.value的
鉴于你的榜样,这应该是这样的:
> db.xx.findOne()
{
"_id" : ObjectId("515ca2bc57a0887a97cc8d14"),
"name" : "Thiago",
"id" : 2093,
"country" : "Portugal",
"custom_data" : [
{
"type" : "company",
"value" : "foo"
},
{
"type" : "plan",
"value" : "pro"
},
{
"type" : "department",
"value" : "it"
},
{
"type" : "sessions",
"value" : 203
}
]
}
> db.xx.ensureIndex({country:1,"custom_data.type":1,"custom_data.value":1})
> db.xx.find({country:"Portugal","custom_data.type":"sessions","custom_data.value":{$gt:100}}).explain()
{
"cursor" : "BtreeCursor country_1_custom_data.type_1_custom_data.value_1",
"isMultiKey" : true,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"country" : [
[
"Portugal",
"Portugal"
]
],
"custom_data.type" : [
[
"sessions",
"sessions"
]
],
"custom_data.value" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
]
},
"server" : "Aspire-5750:27017"
}
你有任意文件和任意查询?如果是这样,那么每次执行查询都会变得很慢,因为每个文档都需要被扫描。即使在记忆中的所有事情中,如果没有索引,那也不会很快或很高效。 – WiredPrairie 2013-03-13 00:04:04
@WiredPrairie的权利。我使用elasticsearch来索引我的文档。每个租户都会在ES内部拥有自己的索引。我想知道这种方法是否正确,你认为如何? elasticsearch是这种情况的一个好方法吗? – CHAPa 2013-03-13 00:12:57
鉴于您需要数据类型(> 100),并且无法在每个组合上创建索引(因为每个集合最多有64个索引,并且建议定期保持在16以下),所以我看不到有效/在MongoDB中对此进行建模的逻辑方法。可能有一个,但并不明显。 – WiredPrairie 2013-03-13 00:19:16