gpt4 book ai didi

mongodb - PHP MongoDB 使用跳过和限制命令不同

转载 作者:可可西里 更新时间:2023-11-01 09:13:17 26 4
gpt4 key购买 nike

有没有办法在 MongoDB (PHP) 中订购不同的查询?如果我可以在其上应用 skip() 和 limit() 会更好。但我还没有找到任何方法来做到这一点。

在 SQL 中,我会按照以下方式做这样的事情:

SELECT distinct(fieldname) 
FROM tablename
ORDER BY fieldname
LIMIT 10
OFFSET 100

最佳答案

您可以使用新的 Aggregation Framework 实现此类查询在 MongoDB 2.2 中:

db.collname.aggregate(
{ $group : { _id : "$fieldname" }},
{ $sort : { _id: 1 }},
{ $skip : 100 },
{ $limit : 10 }
);

有一些limitations尤其要注意:

  • 结果在线返回,因此不能超过最大文档大小(MongoDB 2.2 为 16Mb)
  • 如果任何单个聚合操作消耗超过 10% 的系统 RAM,该操作将产生错误

关于mongodb - PHP MongoDB 使用跳过和限制命令不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11540504/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com