Published at May 2, 2020 · 1 min read
Mongoose 作为 Nodejs 为 MongoDB 构建 Model 层的一个库,使用较为方便,其中的 Ref 与传统 SQL 中的外键较为类似,在 query 时,只需加上 populate 即可将相关的字段查找出来。 但其中有一项需要注意的是: Populate does support a limit option, however, it currently does not limit on a per-document basis. For example, suppose you have 2 stories: If you were to populate() using the limit option, you would find that the 2nd story has 0 fans: That's because, in order to avoid executing a separate query for each document, Mongoose instead queries for fans using numDocuments * limit as the limit....