嗨,有人可以解释我如何为此简单查询制作复合索引?
find({
"userId":{"$in":["5c778c4f52732c06d3384269","5b274b672a35d168960b1bd5","5bc43d6552732c527345c35d","58ec836e899753a22b95fb19"]},
"end":{"$gte": ISODate('2019-05-03 11:05:00+02:00')}
})
我完成了两个化合物索引:
- 用户ID:1 结束:1
- 用户ID:1 结束:-1
但是查询不使用其中任何一个。此索引怎么了?
这是dumend((
的结果{
"queryPlanner": {
"plannerVersion": 1,
"namespace": "WebApp.SessionLog",
"indexFilterSet": false,
"parsedQuery": {
"$and": [
{
"end": {
"$gte": ISODate("2019-05-03T07:05:00Z")
}
},
{
"userId": {
"$in": [
"58ec836e899753a22b95fb19",
"5b274b672a35d168960b1bd5",
"5bc43d6552732c527345c35d",
"5c778c4f52732c06d3384269"
]
}
}
]
},
"winningPlan": {
"stage": "FETCH",
"filter": {
"userId": {
"$in": [
"58ec836e899753a22b95fb19",
"5b274b672a35d168960b1bd5",
"5bc43d6552732c527345c35d",
"5c778c4f52732c06d3384269"
]
}
},
"inputStage": {
"stage": "IXSCAN",
"keyPattern": {
"end": -1
},
"indexName": "end",
"isMultiKey": false,
"multiKeyPaths": {
"end": []
},
"isUnique": false,
"isSparse": false,
"isPartial": false,
"indexVersion": 2,
"direction": "forward",
"indexBounds": {
"end": [
"[new Date(9223372036854775807), new Date(1556867100000)]"
]
}
}
},
"rejectedPlans": [
{
"stage": "FETCH",
"inputStage": {
"stage": "IXSCAN",
"keyPattern": {
"userId": 1,
"end": -1
},
"indexName": "userId_1_end_-1",
"isMultiKey": false,
"multiKeyPaths": {
"userId": [],
"end": []
},
"isUnique": false,
"isSparse": false,
"isPartial": false,
"indexVersion": 2,
"direction": "forward",
"indexBounds": {
"userId": [
"["58ec836e899753a22b95fb19", "58ec836e899753a22b95fb19"]",
"["5b274b672a35d168960b1bd5", "5b274b672a35d168960b1bd5"]",
"["5bc43d6552732c527345c35d", "5bc43d6552732c527345c35d"]",
"["5c778c4f52732c06d3384269", "5c778c4f52732c06d3384269"]"
],
"end": [
"[new Date(9223372036854775807), new Date(1556867100000)]"
]
}
}
},
{
"stage": "FETCH",
"inputStage": {
"stage": "IXSCAN",
"keyPattern": {
"userId": 1,
"end": 1
},
"indexName": "userId_1_end_1",
"isMultiKey": false,
"multiKeyPaths": {
"userId": [],
"end": []
},
"isUnique": false,
"isSparse": false,
"isPartial": false,
"indexVersion": 2,
"direction": "forward",
"indexBounds": {
"userId": [
"["58ec836e899753a22b95fb19", "58ec836e899753a22b95fb19"]",
"["5b274b672a35d168960b1bd5", "5b274b672a35d168960b1bd5"]",
"["5bc43d6552732c527345c35d", "5bc43d6552732c527345c35d"]",
"["5c778c4f52732c06d3384269", "5c778c4f52732c06d3384269"]"
],
"end": [
"[new Date(1556867100000), new Date(9223372036854775807)]"
]
}
}
},
{
"stage": "FETCH",
"filter": {
"end": {
"$gte": ISODate("2019-05-03T07:05:00Z")
}
},
"inputStage": {
"stage": "IXSCAN",
"keyPattern": {
"userId": 1
},
"indexName": "userId",
"isMultiKey": false,
"multiKeyPaths": {
"userId": []
},
"isUnique": false,
"isSparse": false,
"isPartial": false,
"indexVersion": 2,
"direction": "forward",
"indexBounds": {
"userId": [
"["58ec836e899753a22b95fb19", "58ec836e899753a22b95fb19"]",
"["5b274b672a35d168960b1bd5", "5b274b672a35d168960b1bd5"]",
"["5bc43d6552732c527345c35d", "5bc43d6552732c527345c35d"]",
"["5c778c4f52732c06d3384269", "5c778c4f52732c06d3384269"]"
]
}
}
}
]
},
"serverInfo": {
"host": "mongo1",
"port": 9000,
"version": "3.4.2",
"gitVersion": "3f76e40c105fc223b3e5aac3e20dcd026b83b38b"
},
"ok": 1
}
这是文档示例:
{
"_id": ObjectId("56c49c147569943e7d4e92f4"),
"ip": "213.81.143.50",
"count": 17,
"end": ISODate("2014-12-02T08:39:56Z"),
"userId": "546dda33899753840584752b",
"date": ISODate("2014-12-02T08:36:47Z"),
"logs": [
{
"parameters": {
"action": "dashboard",
"id": null,
"package": ""
},
"ip": "213.81.143.50",
"date": ISODate("2014-12-02T08:36:47Z"),
"presenter": "Dashboard",
"action": "dashboard"
},
{
"parameters": {
"action": "dashboard",
"id": null,
"backlink": ""
},
"ip": "213.81.143.50",
"date": ISODate("2014-12-02T08:36:48Z"),
"presenter": "Dashboard",
"action": "dashboard"
}
]
}
谢谢。
ps:此PS文本仅用于stackoverflow编辑器错误。
在查询说明结果中您可以在winningPlan
下看到" indexname":" end"。这是指带有end
名称的查询索引。