如何在 PHP 中执行 MongoDB 控制台式查询? [英] How do I do MongoDB console-style queries in PHP?

查看:54
本文介绍了如何在 PHP 中执行 MongoDB 控制台式查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 MongoDB 查询从 javascript 控制台获取到我的 PHP 应用程序中.我试图避免的是必须将查询转换为 PHP本机驱动程序"的格式......我不想手动构建数组和手动链函数,就像我想手动构建一个数组一样MySQL的内部查询结构只是为了获取数据.

I'm trying to get a MongoDB query from the javascript console into my PHP app. What I'm trying to avoid is having to translate the query into the PHP "native driver"'s format... I don't want to hand build arrays and hand-chain functions any more than I want to manually build an array of MySQL's internal query structure just to get data.

我已经有了一个字符串,可以在 Mongo 控制台中生成我想要的确切内容:

I already have a string producing the exact content I want in the Mongo console:

db.intake.find({"processed": {"$exists": "false"}}).sort({"insert_date": "1"}).limit(10);

问题是,有没有办法让我将这个字符串按原样交给 MongoDB 并让它返回一个带有我请求的数据集的游标?

The question is, is there a way for me to hand this string, as is, to MongoDB and have it return a cursor with the dataset I request?

现在我处于编写您自己的解析器,因为将有效 Mongo 查询的子集转换为 PHP 本地驱动程序想要的格式是无效的 json 是无效的"状态,这不是很有趣.

Right now I'm at the "write your own parser because it's not valid json to kinda turn a subset of valid Mongo queries into the format the PHP native driver wants" state, which isn't very fun.

我不想要 ORM 或庞大的包装库;我只想给一个函数我的查询字符串,因为它存在于控制台中,并返回一个我可以使用的迭代器.我知道有几个基于 PHP 的 Mongo 管理器应用程序显然采用控制台样式的查询并处理它们,但最初浏览它们的代码时,我不确定它们如何处理翻译.

I don't want an ORM or a massive wrapper library; I just want to give a function my query string as it exists in the console and get an Iterator back that I can work with. I know there are a couple of PHP-based Mongo manager applications that apparently take console-style queries and handle them, but initial browsing through their code, I'm not sure how they handle the translation.

我非常喜欢在控制台中使用 mongo,但我很快就开始厌恶将每个查询转换为本地作者想要的格式的想法......

I absolutely love working with mongo in the console, but I'm rapidly starting to loathe the thought of converting every query into the format the native writer wants...

推荐答案

我不想手工构建数组和手工链函数...

I don't want to hand build arrays and hand-chain functions...

您的 shell 代码充满了手工构建字典"手工链函数".所有这些点都是链式函数,所有 JSON 都代表字典/哈希表.

Your shell code is filled with "hand build dictionaries" and "hand-chain functions". All of those dots are chained functions and all of the JSON represent dictionaries / hash tables.

让我们做一个快速的比较.

Let's do a quick comparison.

Javascript:

db.intake
  .find({"processed": {"$exists": false}})
  .sort({"insert_date": "1"})
  .limit(10);

PHP:

db->intake
  ->find(array('processed'=> array('$exists'=> false)))
  ->sort(array('insert_date'=> '1'))
  ->limit(10);

所以我基本换了

  • 带有箭头"的点"
  • 冒号"和双箭头"
  • 左大括号"与数组("
  • 带有)"的右大括号"

听起来您真的对 PHP 很生气.我可以理解 PHP 可以是一种迟钝的语言.但是,当涉及到 MongoDB PHP 驱动程序时,其语法尽可能接近原始"javascript.

It sounds like you're really angry at PHP. And I can understand that PHP can be an obtuse language. However, when it comes to the MongoDB PHP driver, the syntax is as close as humanly possible to the "original" javascript.

这篇关于如何在 PHP 中执行 MongoDB 控制台式查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆