I am having following sample document in the mongoDB.
{
"location" : {
"language" : null,
"country" : "null",
"city" : "null",
"state" : null,
"continent" : "null",
"latitude" : "null",
"longitude" : "null"
},
"request" : [
{
"referrer" : "direct",
"url" : "http://www.google.com/"
"title" : "index page"
"currentVisit" : "1401282897"
"visitedTime" : "1401282905"
},
{
"referrer" : "direct",
"url" : "http://www.stackoverflow.com/",
"title" : "index page"
"currentVisit" : "1401282900"
"visitedTime" : "1401282905"
},
......
]
"uuid" : "109eeee0-e66a-11e3"
}
Note:
- The database contains more than
10845document - Each document contains nearly
100request(100 object in the request array). Technology/Language -
node.jsI had
setProfilingto check the execution timeFirst Query - 13899ms Second Query - 9024ms Third Query - 8310ms Fourth Query - 6858msThere is no much difference using indexing
Queries:
I am having the following aggregation queries to be executed to fetch the data.
var match = {"request.currentVisit":{$gte:core.getTime()[1].toString(),$lte:core.getTime()[0].toString()}};
For Example: var match = {"request.currentVisit":{$gte:"1401282905",$lte:"1401282935"}};
For third and fourth query request.visitedTime instead of request.currentVisit
First
[ { "$project":{ "request.currentVisit":1, "request.url":1 }}, { "$match":{ "request.1": {$exists:true} }}, { "$unwind": "$request" }, { "$match": match }, { "$group": { "_id": { "url":"$request.url" }, "count": { "$sum": 1 } }}, { "$sort":{ "count": -1 } } ]Second
[ { "$project": { "request.currentVisit":1, "request.url":1 }}, { "$match": { "request":{ "$size": 1 } }}, { "$unwind": "$request" }, { "$match": match }, { "$group": { "_id":{ "url":"$request.url" }, "count":{ "$sum": 1 } }}, { "$sort": { "count": -1} } ]Third
[ { "$project": { "request.visitedTime":1, "uuid":1 }}, { "$match":{ "request.1": { "$exists": true } }}, { "$match": match }, { "$group": { "_id": "$uuid", "count":{ "$sum": 1 } }}, { "$group": { "_id": null, "total": { "$sum":"$count" }} }} ]Forth
[ { "$project": { "request.visitedTime":1, "uuid":1 }}, { "$match":{ "request":{ "$size": 1 } }}, { "$match": match }, { "$group": { "_id":"$uuid", "count":{ "$sum": 1 } }}, { "$group": { "_id":null, "total": { "$sum": "$count" } }} ]
Problem:
It is taking more than 38091 ms to fetch the data.
Is there any way to optimize the query?
Any suggestion will be grateful.
requestsub-documents? Sure if it's applicable to your application.matchvariable actually is. There are clear problems in these queries that are easy to answer but not complete without knowing that detail.matchvariable inside the$matchpipeline stage. That way we know what your actual condition is. It is not listed in your code above