MongoDb/php 获取集合上的最大 id 值

发布于 2024-12-28 09:05:58 字数 315 浏览 3 评论 0原文

我想获取集合的最大 id 值。

将 mongoDb 查询转换

db.tweets.find({},{id:1}).sort({id:-1}).limit(1)

如何使用 PHP

为 Mongo 查询语言语句?我正在尝试

$db->tweets->find(
    array(),
    array("id"=>1)
)->sort(array("id"=> -1))->limit(1);

,但这不起作用。

I want to get the max id value on a collection.

How do I convert the mongoDb query:

db.tweets.find({},{id:1}).sort({id:-1}).limit(1)

to Mongo Query Language Statement using PHP?

I'm trying

$db->tweets->find(
    array(),
    array("id"=>1)
)->sort(array("id"=> -1))->limit(1);

but that doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2025-01-04 09:05:58

我检查了这个,它对我有用:

$val = $db->myCollection->find(array(), array('_id' => 1))->sort(array('_id' => -1))->limit(1);

代码中的错误是它应该是“_id”而不是“id”。另外,我希望 $db->tweets 是一个 MongoCollection 对象,并且您已经确保了这一点。希望这有帮助。

I checked this and it works for me:

$val = $db->myCollection->find(array(), array('_id' => 1))->sort(array('_id' => -1))->limit(1);

The error in your code is that it should be "_id" and not "id". Also, I hope $db->tweets is a MongoCollection object and you have ensured this. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文