Mongoose populate limit 的正确用法

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. If you need the correct limit, you should use the perDocumentLimit option (new in Mongoose 5.9.0). Just keep in mind that populate() will execute a separate query for each story.

若想约束每个“外键”返回的数量,则不能使用 limit,如果用 limit 则会出现后续的结果中并没有返回值,若想严格约束,则需使用 perDocumentLimit.

comments powered by Disqus


abcs accept acid activemq affinity algorithm allocation android array async aws b+tree b-tree backoff benchmark best-practices bfs big-o bigquery bind bitcount blog break broker bubble buffer cache cap cert cgroups channel citus class classmethod closure closures cluster concurrency config consistency consumer container context cookie cors crawler cronjob csrf ctr data-science data-structure database datadog dataflow datascience decorator deepcopy defer dfs distributed django dns docker double-shipping drf ecosia elastic-search enumerate epoll equal errgroup escaping event extra fabric2 facebook-pixel financial-report flask flutter forward-proxy freelance frontend frp garbage-collector gc gcp generator gesture get gil git golang goroutine graphql ha handbook haproxy hash hash-slot hashring hashtable hpa http http-auth http-proxy http_proxy https index init innodb instagram intention-lock intergration interview ios is javascript jinja2 jobboard json jwt k8s kafka kibana kqueue label lambda layer4 layer7 lean linked-list linux list listen loadbalancer lock logs long-tail lru marketing master matplotlib memory merge metaclass metaprogramming metrics metrics-server microservices mitm mobile model mongo mongoose mq myisam mysql namespace nat netflix network network-extension nginx nodejs nomad nosql notification npm oodesign openssl optimization orm osi pandas parallelism paramiko parkinglot patroni permission pg pipeline pixelme post postgresql postresql prefetch_related prerender private-key process proxy proxycommand put pvm python queue rabbitmq rbac react-native reactive reactjs rebase redis redis-cluster replication resource rest restfulapi retargeting retry revenue reverse-proxy rocketmq rsa rxswift saas scaffold scaleable search-engine security select seo serverless service session set shadosocks shadowsocks shard sharding shell shopify sigint signal sigterm singleton slack slave slow-query sniper sns socket socks5 source-code spa sql sqlalchemy sqs ssh ssl ssl-pinning stack startup state stateful stateless staticmethod string struct swift swiftui switch syscall system-design systemctl tcp tcp-proxy thread tmpreaper token traefik trustkit tunning type typeform udp userdefaults variable vc voidcallback vpn vuejs weak web web-development where widget with yarn zset 削峰 单例模式 宽索引 异步 看源码学-golang 窄索引 解耦 跨域 跳板机


2020 (6)
2019 (157)
0001 (5)