我可以在 node-mongodb-native 驱动程序中执行原始 MongoDB 查询吗? [英] Can I execute a raw MongoDB query in node-mongodb-native driver?

查看:42
本文介绍了我可以在 node-mongodb-native 驱动程序中执行原始 MongoDB 查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅供参考 - 我知道如何使用 MongoDB 驱动程序,并且知道这不是在 Web 应用程序中使用它的方式,但这不适用于 Web 应用程序.我的目标是在 NodeJS 中模拟 MongoDB shell

我正在编写一个 DB GUI 并且想要执行一个原始的 MongoDB 查询,例如 db.tableName.find({ col: 'value' }).我可以使用 本机 MongoDB 驱动程序来实现这一点吗?我使用的是 v2.2,这是当前的最新版本.

I'm writing a DB GUI and would like to execute a raw MongoDB query, eg db.tableName.find({ col: 'value' }). Can I achieve this using the native MongoDB driver? I'm using v2.2, which is current the latest version.

如果没有,我如何在 NodeJS 中实现这一点?

If not, how can I achieve this in NodeJS?

推荐答案

注意:问题已更改 - 请参阅下面的更新.

原答案:

是的.

代替:

db.tableName.find({ col: 'value' })

您将其用作:

db.collection('tableName').find({ col: 'value' }, (err, data) => {
    if (err) {
        // handle error
    } else {
        // you have data here
    }
});

参见:http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#find

在您更改问题并发表评论后,您想做什么就更清楚了.

After you changed your question and posted some comments it is more clear what you want to do.

要实现在 Node 中模拟 Mongo shell 的目标,您需要解析用户键入的命令并执行适当的命令,同时牢记:

To achieve your goal of emulating the Mongo shell in Node you would need to parse the command typed by the user and execute the appropriate command while keeping in mind:

  1. Mongo shell 使用的 SpiderMonkey 与 Node with V8 和 libuv 的区别
  2. BSON 和 JSON 的区别
  3. Mongo shell 同步工作而 Node 驱动程序异步工作的事实

最后一部分对您来说可能是最难的部分.请记住,在 Mongo shell 中这是完全合法的:

The last part will probably be the hardest part for you. Remember that in the Mongo shell this is perfectly legal:

db.test.find()[0].x;

在 Node 中,.find() 方法不返回值,但它接受回调或返回承诺.这会很棘手.db.test.find()[0].x; 使用 promise 可能相对容易处理(如果你很好地理解了 promise),但这会更难:

In Node the .find() method doesn't return the value but it either takes a callback or returns a promise. It will be tricky. The db.test.find()[0].x; case may be relatively easy to handle with promises (if you understand the promises well) but this will be harder:

db.test.find({x: db.test.find()[0].x});

并记住您需要处理任意嵌套的级别.

and remember that you need to handle arbitrarily nested levels.

在阅读了一些评论后,我认为值得注意的是,您实际发送到 Mongo 服务器的内容与您在 Mongo shell 中编写的 JavaScript 无关.Mongo shell 使用 SpiderMonkey 和许多预定义的函数和对象.

After reading some of the comments I think it's worth noting that what you actually send to the Mongo server has nothing to do with the JavaScript that you write in the Mongo shell. The Mongo shell uses SpiderMonkey with a number of predefined functions and objects.

但您实际上并未将 JavaScript 发送到 Mongo 服务器,因此您无法发送诸如 db.collection.find() 之类的内容.相反,您发送一个二进制 OP_QUERY 结构,其中包含编码为 cstring 的集合名称和编码为 BSON 的查询以及一堆二进制标志.见:

But you don't actually send JavaScript to the Mongo server so you can't send things like db.collection.find(). Rather you send a binary OP_QUERY struct with a collection name encoded as a cstring and a query encoded as BSON plus a bunch of binary flags. See:

BSON 本身是一种二进制格式,其中包含许多定义为字节的低级值:

The BSON is itself a binary format with a number of low level values defined as bytes:

最重要的是,您不会向 Mongo 服务器发送与您在 Mongo shell 中输入的内容类似的任何内容.Mongo shell 使用 SpiderMonkey 解析器解析您输入的内容,并将二进制请求发送到实际的 Mongo 服务器.Mongo shell 使用 JavaScript,但您不使用 JavaScript 与 Mongo 服务器通信.

The bottom line is that you don't send to the Mongo server anything resembling what you enter in the Mongo shell. The Mongo shell parses the things that you type using the SpiderMonkey parser and sends binary requests to the actual Mongo server. The Mongo shell uses JavaScript but you don't communicate with the Mongo server in JavaScript.

即使是 JSON 查询对象也不会作为 JSON 发送到 Mongo.例如,当您搜索具有等于world"的 hello 属性的文档时,您将在 JavaScript 或 { 中使用 {hello: 'world'}"hello": "world"} 在 JSON 中,但这是通过 Mongo shell 或任何其他 Mongo 客户端发送到 Mongo 服务器的内容:

Even the JSON query object is not sent to Mongo as JSON. For example, when you are searching for a document with a hello property equal to "world" you would use {hello: 'world'} in JavaScript or {"hello": "world"} in JSON but this is what gets send to the Mongo server - by the Mongo shell or by any other Mongo client:

\x16\x00\x00\x00\x02hello\x00\x06\x00\x00\x00world\x00\x00   

为什么如此不同

要了解 Node 中使用的语法为何与 Mongo shell 如此不同,请参阅此答案:

Why it's so different

To understand why the syntax used in Node is so different from the Mongo shell, see this answer:

这篇关于我可以在 node-mongodb-native 驱动程序中执行原始 MongoDB 查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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