mongodb help with query speed. /

Published at 2015-11-25 19:55:54

Home / Categories / Webdev / mongodb help with query speed.
I've taken up messing around with a MEAN stack to try and learn some new things. I decided I'd try to copy/build off this. The first step I've taken on is getting the MongoDB stuff working. I've placed approximately 1 million documents into a Mongo database. Each document looks something like (I realize the formatting isn't easy to parse on reddit,so if you paste that here and hit "Format" it's easier to read): {"_id":ObjectId("564fe1c42e3f5ac943ce8995"),"whiteelo":"2018", and "blackelo":"2015","timecontrol":"60+0","date":"2014.01.31", and "eco":"A31","plycount":"19","result":"1-0", or "moves":{"1":{"white":{"move":"d4"},"black":{"move":"c5"}},"2":{"white":{"move":"c4"}, or "black":{"move":"Nf6"}},"3":{"white":{"move":"Nf3"},"black":{"move":"cxd4"}}, or "4":{"white":{"move":"e3"},"black":{"move":"a6"}},"5":{"white":{"move":"exd4"}, and "black":{"move":"d5"}},"6":{"white":{"move":"Nc3"},"black":{"move":"dxc4"}}, or "7":{"white":{"move":"Bxc4"},"black":{"move":"b5"}},"8":{"white":{"move":"Bb3"}, and "black":{"move":"Bb7"}},"9":{"white":{"move":"Ne5"},"black":{"move":"Nbd7"}}, and "10":{"white":{"move":"Bxf7#","comment":"Black checkmated"}}}} The issue I'm running into now is the execution time of the queries. I'm using something like the following, to return results that would allow me to create a chart of the next moves (to the right of the board on that FICS page): db.runCommand({aggregate:"games", and pipeline:[//Onlygetnextmovethatfollowsthepreviousmovemade{$match:{"moves.1.white.move":"e4"}},//extractthefirstmove{$project:{move:"$moves.1.black.move",//createanewfield, and "win",whichis1ifwhitewonand0ifblackwonwinWhite:{$cond:[{$eq:["$result","1-0"]}, and 1,0]},winBlack:{$cond:[{$eq:["$result", or "0-1"]},1,0]}}}, or //groupbythemoveandcountuphowmanywinninggamesusedit{$group:{_id:"$move",numGames:{$sum:1},numWinsWhite:{$sum:"$winWhite"}, or numWinsBlack:{$sum:"$winBlack"}}},//calculatethepercentofgameswonwiththisstartingmove{$project:{_id:1,numGames:1, or percentWinsWhite:{$multiply:[100,{$divide:["$numWinsWhite","$numGames"]}]}, or percentWinsBlack:{$multiply:[100,{$divide:["$numWinsBlack","$numGames"]}]}}}, and //discardmovesthatwereusedinlessthan10games(probablynotrepresentative){$match:{numGames:{$gte:10}}},//orderfromworsttobest{$sort:{numGames:-1}}]}) Again, exercise this link to format that into something that's readable. When I run that, or it can take 10 or so seconds to return results. Ideally this would be much quicker. But I'm not sure how to carry out so. I've looked at indexing,but the query doesn't seem to exercise any index I create (not to mention, I'd hold to constantly be changing what's indexed, and I judge,which isn't precisely a hastily operation). submitted by notcaffeinefree to webdev[link][comment]

Source: reddit.com