In my article detail page require a random 3 records similar to this article.
Before I use mysql as database, and query by “order by rand() limit 3”, however my application crashed because mysql exausted the vps memery when a rogue search-engine scrapying my website by more than 4 concurrency. I am so disappoint!!
First of all, the vps I used the aliyun’s ecs, the ability is very weak. Some colleague suggest me to create the html file to solve the problem. I don’t want to do that. I want a dynamic application rather than a static one.
So I turn to mongodb instead of mysql.
I use https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/stable/reference/aggregation-builder.html#aggregation-builder
It is called odm. I am not familar to this.
I just wanna 3 random recoards which has the same class with the current detail.
Any big gun can help on this?
I followed and changed a little according to the demo as the above linke. However what I got is not the record data, I don’t know what I got, it seems a mess of codes, and when I print_r it, it keep outputting for a long time and almost cause a web browser crash.
thank you in advance.
ps: getPrev and getNext work well, just the getSimilar works not as I expected.
I run db.article.aggregate([{ $sample: { size: 3 }}])
in local (I mean on my work computer of windows 10 with a mongodb server) envirment, it takes several micro second. However when I run it in the online vps(very cheap one, ubuntu 16.04 server with mongod) it takes almost 6 seconds!! no matter the time it takes, first of all I wanna to make the code correct and get it run the right way.