In my mongodb collection i have 15 million documents with following json structure. The embedded document count inside playfields array field changes for each json document. All my queries involves filtering the documents based on data in playfields array field. All queries are taking more than 2 minutes to execute.
The value field in embedded document stores multiple datatypes (int, string). Is this bad design?
Am i doing anything wrong in writing queries? Am I missing any indexes? Do I have to move data from embed documents in single document to multiple collections?
The multiple conditions query(posted in question) takes 3 minutes to execute. Is the syntax wrong to use when filtering same collection? My goal is return docs that meets all these conditions.
If I break the query into piece each took ms to execute. 1) db.playfieldvalues.find({$or:[ {playfields: {$elemMatch:{ID:"Play.NHL.NHLAwayTeam" ,value: "NYI NEW YORK ISLANDERS"}}},{playfields: {$elemMatch:{ID:"Play.NHL.NHLAwayTeam" ,value: "T.B TAMPA BAY LIGHTNING"}}}]}) 2) db.playfieldvalues.find({playfields: {$elemMatch:{ID:"Play.NHL.NHLHomeTeam" ,value: "BOS BOSTON BRUINS"}}}) 3) db.playfieldvalues.find({playfields: {$elemMatch:{ID:"Play.NHL.NHLEventX" ,value: {$gt: 0, $lt: 25}}}}) 4) db.playfieldvalues.find({playfields: {$elemMatch:{ID:"Play.NHL.NHLEventScoreDifferential" ,value: {$gt: 0}}}})
Index created:
db.collection.ensureIndex({ "playfields.ID": 1, "playfields.value": 1 })
queries running:
1:
db.playfieldvalues.find({playfields: {$elemMatch:{ID:"Play.NHL.NHLHomeTeam" ,value: "BOS BOSTON BRUINS"}}})
2:
db.playfieldvalues.find({$and:[
{playfields: {$elemMatch:{ID:"Play.NHL.NHLHomeTeam" ,value: "BOS BOSTON BRUINS"}}},
{$or:[ {playfields: {$elemMatch:{ID:"Play.NHL.NHLAwayTeam" ,value: "NYI NEW YORK ISLANDERS"}}},{playfields: {$elemMatch:{ID:"Play.NHL.NHLAwayTeam" ,value: "T.B TAMPA BAY LIGHTNING"}}}]},
{playfields: {$elemMatch:{ID:"Play.NHL.NHLEventY" ,value: -38}}},
{playfields: {$elemMatch:{ID:"Play.NHL.NHLEventX" ,value: {$gt: 0}}}},
{playfields: {$elemMatch:{ID:"Play.NHL.NHLEventScoreDifferential" ,value: {$gt: 0}}}}
]
})
JSON Document sample:
{
"_id" : ObjectId("59dbd4c5704aa82e70ac10b5"),
"playid" : "2594c658-aa3b-4a98-b2eb-0cc03e4dc9e5",
"playfields" : [
{
"ID" : "Play.NHL.NHLGameDate",
"TS" : "",
"value" : NumberInt(20160228)
},
{
"ID" : "Play.GameDate",
"TS" : "",
"value" : "2/28/2016 12:00:00 AM"
},
{
"ID" : "Play.NHL.NHLEventType",
"TS" : "",
"value" : "HIT"
},
{
"ID" : "Play.NHL.NHLClockTime",
"TS" : "",
"value" : "03:08"
},
{
"ID" : "Play.NHL.NHLClockTimeSeconds",
"TS" : "",
"value" : NumberInt(188)
},
{
"ID" : "Play.NHL.NHLEventX",
"TS" : "",
"value" : NumberInt(62)
},
{
"ID" : "Play.NHL.NHLEventY",
"TS" : "",
"value" : NumberInt(-38)
},
{
"ID" : "Play.NHL.NHLEventPeriod",
"TS" : "",
"value" : "1"
},
{
"ID" : "Play.NHL.NHLGameCode",
"TS" : "",
"value" : "20933"
},
{
"ID" : "Play.NHL.NHLSeason",
"TS" : "",
"value" : "20152016"
},
{
"ID" : "Play.NHL.NHLHomeTeam",
"TS" : "",
"value" : "BOS BOSTON BRUINS"
},
{
"ID" : "Play.NHL.NHLAwayTeam",
"TS" : "",
"value" : "T.B TAMPA BAY LIGHTNING"
},
{
"ID" : "Play.NHL.NHLPrimaryTeam",
"TS" : "",
"value" : "T.B TAMPA BAY LIGHTNING"
},
{
"ID" : "Play.NHL.NHLPrimaryTeamActionPlayer",
"TS" : "",
"value" : "e27ca5e6-d4fa-4d45-8fa2-a860f64f7ea7"
},
{
"ID" : "Play.NHL.NHLSecondaryTeam",
"TS" : "",
"value" : "BOS BOSTON BRUINS"
},
{
"ID" : "Play.NHL.NHLSecondaryTeamActionPlayer",
"TS" : "",
"value" : "bea1deb6-aabd-47e8-b216-6f4df5f1ea97"
},
{
"ID" : "Play.NHL.NHLEventZone",
"TS" : "",
"value" : "DZ"
},
{
"ID" : "Play.NHL.NHLEventScoreDifferential",
"TS" : "",
"value" : NumberInt(1)
},
{
"ID" : "Play.NHL.NHLEventStrength",
"TS" : "",
"value" : "Even"
}
]
}