如何在MongoDB聚合查询中使用$hint?
我正在ubuntu机器上使用mongo v3.0.1。我收集了3亿行。我已经根据我的查询偏好创建了两个索引 当我尝试使用explain运行聚合时,它使用的是低效的索引,这就是为什么它要多花20-25秒的时间。是否有方法放置如何在MongoDB聚合查询中使用$hint?,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我正在ubuntu机器上使用mongo v3.0.1。我收集了3亿行。我已经根据我的查询偏好创建了两个索引 当我尝试使用explain运行聚合时,它使用的是低效的索引,这就是为什么它要多花20-25秒的时间。是否有方法放置$hint,以便我的聚合查询使用适当的索引 $match处于我的第一个管道阶段。我有两个索引: “主机\ 1 \站点类型\ 1” “访问时间\uU1\u帐户ID\uU1\u主机\uU1\u站点类型\uU1\u扩展\uU1\u生存期\uU1” 我的$match管道如下所示: {“
$hint
,以便我的聚合查询使用适当的索引
$match
处于我的第一个管道阶段。我有两个索引:
$match
管道如下所示:
{“$match”:{
“AccountId”:accID,
“访问时间”:{“$lte”:今天,$gte:最后365天},
“$or”:[
{“$and”:[
{“扩展”:{“$in”:[“chrome_0”,“firefox_0”]},
{“生存期”:0}
]},
{“寿命”:{“$gt”:1000}
],
“主机”:{“$ne”:“本地主机”},
“SiteType”:{“$exists”:true},
}
它使用的是第一个索引,而不是第二个索引。第一个索引花费的时间是50秒,而仅仅使用第二个索引只需要18秒
这是我的一个文档示例:
{
“_id”:“2bc1143c-07e4-4c37-a020-a7485b2802a3”,
“CreatedDate”:ISODate(“2015-07-22T04:05:06.802+0000”),
“更新日期”:ISODate(“2015-07-22T05:28:26.469+0000”),
“AccountId”:accID,
“Url”:”http://www.test.com/test.html",
“主机”:“test.com”,
“访问时间”:ISODate(“2014-08-12T18:08:25.813+0000”),
“寿命”:789546.01,
“状态”:“已关闭”,
“本地时间”:ISODate(“2014-08-12T18:08:25.813+0000”),
“设备ID”:“123456789”,
“扩展”:“firefox_0”,
“子网站类型”:“TestSubSite”,
“站点类型”:“测试站点”,
“标志”:“1”
}
下面是我的解释:
{
“阶段”:[
{
“$cursor”:{
“查询”:{
“帐户ID”:“帐户ID”,
“访问时间”:{
“$lte”:“2015-07-25T18:30:00Z”,
“$gte”:“2014-07-25T18:30:00Z”
},
“主持人”:{
$ne:“本地主机”
},
“站点类型”:{
“$exists”:true
},
“$or”:[
{
“$and”:[
{
“扩展”:{
“$in”:[
“chrome_0”,
“firefox_0”
]
}
},
{
“寿命”:0
}
]
},
{
“终生”:{
“$gt”:1000
}
}
]
},
“字段”:{
"主持人":1,,
“\u id”:0
},
“queryPlanner”:{
“规划版本”:1,
“名称空间”:“测试”,
“indexFilterSet”:false,
“parsedQuery”:{
“$and”:[
{
“$or”:[
{
“$and”:[
{
“终生”:{
“$eq”:0
}
},
{
“扩展”:{
“$in”:[
“chrome_0”,
“firefox_0”
]
}
}
]
},
{
“终生”:{
“$gt”:1000
}
}
]
},
{
“$not”:{
“主持人”:{
$eq:“本地主机”
}
}
},
{
“访问时间”:{
$lte:“2015-07-25T18:30:00Z”
}
},
{
“帐户ID”:{
$eq:“帐户ID”
}
},
{
“访问时间”:“2014-07-25T18:30:00Z”
},
{
“站点类型”:{
“$exists”:true
}
}
]
},
“wi
db.collection.aggregate(pipeline, {hint: "index_name"})