理解Node / Mongo中的find [英] Understanding find in Node/Mongo
问题描述
我正在尝试学习节点。
考虑这段代码(基于官方MongoDB Node.js驱动程序)
I am trying to learn node. Consider this code (based on official MongoDB Node.js driver)
// Retrieve all the documents in the collection
collection.find().toArray(function(err, documents) {
assert.equal(1, documents.length);
assert.deepEqual([1, 2, 3], documents[0].b);
db.close();
});
我有两个问题:
- 是
find
同步还是异步? -
如果它是异步的,
.toArray
函数调用让我感到困惑,因为通常情况下我会期待某些内容
- is
find
synchronous or asynchronous? If it is asynchronous, the
.toArray
function call is confusing me, because normally I would expect something along the lines of
collection.find(function(err, results){});
具体来说我感兴趣什么机制允许你在异步函数的结果上调用 .toArray
?因为我得到的异步函数很少返回一些东西(我认为除了promises),而是调用传递给它们的回调。有人可以通过find和 .toArray
澄清这种情况吗?
Specifically I am interested what mechanism allows you to call a .toArray
on result of asynchronous function? Because asynchronous functions as I get it rarely return something (I think except promises), rather invoke callbacks passed to them. Can someone clarify this situation with find and .toArray
?
例如,在这个问题的接受答案中:如何获得回调MongoDB collection.find(),你可以看到作者调用找到
我想象的方式,并收到游标
在回调函数中。这对我很好,这就是我期望它的工作方式。
但链接异步调用的结果查找
(如果是异步?), toArray
有点让我困惑。
For example in the accepted answer of this question: How to get a callback on MongoDB collection.find(), you can see author calls find
the way I envisioned, and received cursor
in callback function. That is fine with me, that is how I expected it to work.
But chaining result of asynchronous call find
(if it is asynch?), with toArray
a bit confuses me.
我的推测是 find
返回一个句柄类的东西,数据此时尚未从DB加载,只有当您调用 toArray
实际数据到达时。我是对的吗?
My speculation is find
returns a handle kind of thing, the data at this point hasn't been loaded from DB, only when you call toArray
the actual data arrives. Am I right?
推荐答案
我承认你,这个案子有点奇怪。这里是mongodb-native的v2.2。
I concede you, this case is a bit weird. Here is for the v2.2 of mongodb-native.
首先, find
有两种不同的用法。你可以给出回调函数。但在无论如何中,它会返回同步一个对象。更准确地说,它是一个光标。
我们可以期待一个异步机制在传递回调但不在这里。
First of all, find
has two different usages. You can either give a callback function or not. But in any case, it returns synchronously an object. More precisely it's a cursor.
We could expect a asynchronous mechanism when passing a callback but not here.
collection.find({ }, function (err, cursor) {
assert(!err);
});
console.log('This happens after collection.find({ }, callback)');
OR
const cursor = collection.find({});
console.log('Also happening after');
另一方面, toArray
是一个异步功能也有两种不同的用法。这次,返回的对象因参数而异。
On the other hand, toArray
is an asynchronous function and has also two different usages. This time, the returned object is different depending on the arguments.
等效:
cursor.toArray(function (err, documents) {
assert.equal(1, documents.length);
});
AND
cursor.toArray()
.then(documents => {
assert.equal(1, documents.length);
});
在第一次调用中, toArray
返回 undefined
而在第二个中,它返回 Promise
。
In the first call, toArray
returns undefined
whereas in the second, it returns a Promise
.
这篇关于理解Node / Mongo中的find的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!