I am working through a MEAN stack tutorial. It contains the following code as a route in index.js
. The name of my Mongo collection is brandcollection
.
我正在通过MEAN堆栈教程。它包含以下代码作为index.js中的路由。我的Mongo系列的名称是brandcollection。
/* GET Brand Complaints page. */
router.get('/brands', function(req, res) {
var db = req.db;
var collection = db.get('brandcollection');
collection.find({},{},function(e,docs){
res.render('brands', {
"brands" : docs
});
});
});
I would like to modify this code but I don't fully understand how the .find
method is being invoked. Specifically, I have the following questions:
我想修改此代码,但我不完全了解如何调用.find方法。具体来说,我有以下问题:
-
What objects are being passed to
function(e, docs)
as its arguments?传递给函数(e,docs)作为参数的对象是什么?
-
Is
function(e, docs)
part of the MongoDB syntax? I have looked at the docs on Mongo CRUD operations and couldn't find a reference to it. And it seems like the standard syntax for a Mongo.find
operation iscollection.find({},{}).someCursorLimit()
. I have not seen a reference to a third parameter in the.find
operation, so why is one allowed here?函数(e,docs)是MongoDB语法的一部分吗?我查看了有关Mongo CRUD操作的文档,但找不到对它的引用。似乎Mongo .find操作的标准语法是collection.find({},{})。someCursorLimit()。我没有在.find操作中看到对第三个参数的引用,为什么一个允许在这里?
-
If
function(e, docs)
is not a MongoDB operation, is it part of the Monk API?如果函数(e,docs)不是MongoDB操作,它是Monk API的一部分吗?
-
It is clear from the tutorial that this block of code returns all of the documents in the collection and places them in an object as an attribute called "brands." However, what role specifically does
function(e, docs)
play in that process?从教程中可以清楚地看出,这段代码返回集合中的所有文档,并将它们作为名为“品牌”的属性放在对象中。但是,功能(e,docs)在这个过程中扮演什么角色呢?
Any clarification would be much appreciated!
任何澄清将非常感谢!
1 个解决方案
#1
12
The first parameter is the query.
第一个参数是查询。
The second parameter(which is optional) is the projection i.e if you want to restrict the contents of the matched documents
第二个参数(可选)是投影,即如果要限制匹配文档的内容
collection.find( { qty: { $gt: 25 } }, { item: 1, qty: 1 },function(e,docs){})
would mean to get only the item
and qty
fields in the matched documents
意味着只获得匹配文档中的item和qty字段
The third parameter is the callback function which is called after the query is complete. function(e, docs)
is the mongodb driver for node.js syntax. The 1st parameter e
is the error. docs
is the array of matched documents. If an error occurs it is given in e
. If the query is successful the matched documents are given in the 2nd parameter docs
(the name can be anything you want).
第三个参数是在查询完成后调用的回调函数。 function(e,docs)是node.js语法的mongodb驱动程序。第一个参数e是错误。 docs是匹配文档的数组。如果发生错误,则在e中给出。如果查询成功,匹配的文档将在第二个参数docs中给出(名称可以是您想要的任何名称)。
The cursor has various methods which can be used to manipulate the matched documents before mongoDB returns them. collection.find( { qty: { $gt: 25 } }, { item: 1, qty: 1 }) is a cursor you can do various operations on it.
游标有多种方法可用于在mongoDB返回之前操作匹配的文档。 collection.find({qty:{$ gt:25}},{item:1,qty:1})是一个游标,你可以对它进行各种操作。
collection.find( { qty: { $gt: 25 } }, { item: 1, qty: 1 }).skip(10).limit(5).toArray(function(e,docs){
...
})
meaning you will skip the first 10 matched documents and then return a maximum of 5 documents.
这意味着您将跳过前10个匹配的文档,然后返回最多5个文档。
All this stuff is given in the docs. I think it's better to use mongoose instead of the native driver because of the features and the popularity.
所有这些东西都在文档中给出。我认为最好使用mongoose而不是本机驱动程序,因为它的功能和受欢迎程度。
#1
12
The first parameter is the query.
第一个参数是查询。
The second parameter(which is optional) is the projection i.e if you want to restrict the contents of the matched documents
第二个参数(可选)是投影,即如果要限制匹配文档的内容
collection.find( { qty: { $gt: 25 } }, { item: 1, qty: 1 },function(e,docs){})
would mean to get only the item
and qty
fields in the matched documents
意味着只获得匹配文档中的item和qty字段
The third parameter is the callback function which is called after the query is complete. function(e, docs)
is the mongodb driver for node.js syntax. The 1st parameter e
is the error. docs
is the array of matched documents. If an error occurs it is given in e
. If the query is successful the matched documents are given in the 2nd parameter docs
(the name can be anything you want).
第三个参数是在查询完成后调用的回调函数。 function(e,docs)是node.js语法的mongodb驱动程序。第一个参数e是错误。 docs是匹配文档的数组。如果发生错误,则在e中给出。如果查询成功,匹配的文档将在第二个参数docs中给出(名称可以是您想要的任何名称)。
The cursor has various methods which can be used to manipulate the matched documents before mongoDB returns them. collection.find( { qty: { $gt: 25 } }, { item: 1, qty: 1 }) is a cursor you can do various operations on it.
游标有多种方法可用于在mongoDB返回之前操作匹配的文档。 collection.find({qty:{$ gt:25}},{item:1,qty:1})是一个游标,你可以对它进行各种操作。
collection.find( { qty: { $gt: 25 } }, { item: 1, qty: 1 }).skip(10).limit(5).toArray(function(e,docs){
...
})
meaning you will skip the first 10 matched documents and then return a maximum of 5 documents.
这意味着您将跳过前10个匹配的文档,然后返回最多5个文档。
All this stuff is given in the docs. I think it's better to use mongoose instead of the native driver because of the features and the popularity.
所有这些东西都在文档中给出。我认为最好使用mongoose而不是本机驱动程序,因为它的功能和受欢迎程度。