仅在子记录密钥中搜索php mongo搜索

发布于 2025-02-06 12:44:55 字数 469 浏览 1 评论 0原文

想在mongo中获取行。

我想匹配php mongo中的子登记件,

我 在Mongo中,

"invoice_id":0,"users":{"14":"2022-06-09 10:56:10","212":"2022-06-09 04:05:35"},
"invoice_id":2,"users":{"15":"2022-06-09 10:56:10","246":"2022-06-09 04:05:35"},    

我想获取用户=“ 14”:

我尝试了php mongo查询

$data["users.0"] = new MongoDB\BSON\Regex("14");
$result = $collection->find($data, $option);    

问题是 - 它的价值搜索。我只想在密钥中搜索。

请指导。

I want to to fetch row in Mongo on matching the subdocument in PHP Mongo.I want to achive result like

mysql query :- users like '"14:"' or invoice_id ="2"`

My structure in Mongo is

"invoice_id":0,"users":{"14":"2022-06-09 10:56:10","212":"2022-06-09 04:05:35"},
"invoice_id":2,"users":{"15":"2022-06-09 10:56:10","246":"2022-06-09 04:05:35"},    

I want to fetch users = "14":

I tried PHP Mongo Query

$data["users.0"] = new MongoDB\BSON\Regex("14");
$result = $collection->find($data, $option);    

The problem is - it search in value. I want to search in key only.

Please guide.

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

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

发布评论

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

评论(1

萝莉病 2025-02-13 12:44:55

解决方案是“存在”,

$data["users.14"] =   ['$exists' => true];
$result = $collection->find($data, $option);

这为搜索结果提供了键“ 14”

The solution was "$exists"

$data["users.14"] =   ['$exists' => true];
$result = $collection->find($data, $option);

This gives search result with Key "14"

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