简体   繁体   English

在Monk上对MongoDB集合使用find方法

[英]Using the find method on a MongoDB collection with Monk

I am working through a MEAN stack tutorial . 我正在研究MEAN堆栈教程 It contains the following code as a route in index.js . 它包含以下代码作为index.js的路由。 The name of my Mongo collection is brandcollection . 我的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. 我想修改这段代码,但是我不完全了解.find方法是如何被调用的。 Specifically, I have the following questions: 具体来说,我有以下问题:

  1. What objects are being passed to function(e, docs) as its arguments? 哪些对象作为参数传递给function(e, docs)

  2. Is function(e, docs) part of the MongoDB syntax? function(e, docs)是MongoDB语法的一部分吗? I have looked at the docs on Mongo CRUD operations and couldn't find a reference to it. 我查看了有关Mongo CRUD操作的文档,但找不到对此的引用。 And it seems like the standard syntax for a Mongo .find operation is collection.find({},{}).someCursorLimit() . 似乎Mongo .find操作的标准语法是collection.find({},{}).someCursorLimit() I have not seen a reference to a third parameter in the .find operation, so why is one allowed here? 我没有在.find操作中看到对第三个参数的.find ,那么为什么在这里允许一个?

  3. If function(e, docs) is not a MongoDB operation, is it part of the Monk API? 如果function(e, docs)不是MongoDB操作,它是否属于Monk API?

  4. 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? 但是, function(e, docs)在该过程中具体扮演什么角色?

Any clarification would be much appreciated! 任何澄清将不胜感激!

The first parameter is the query. 第一个参数是查询。

The second parameter(which is optional) is the projection ie 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 意味着只获取匹配文档中的itemqty字段

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. function(e, docs)是用于node.js语法的mongodb驱动程序。 The 1st parameter e is the error. 第一个参数e是错误。 docs is the array of matched documents. docs是匹配文档的数组。 If an error occurs it is given in e . 如果发生错误,则在e给出。 If the query is successful the matched documents are given in the 2nd parameter docs (the name can be anything you want). 如果查询成功,则在第二个参数docs中提供匹配的文档(名称可以是您想要的任何名称)。

The cursor has various methods which can be used to manipulate the matched documents before mongoDB returns them. 游标具有多种方法,可用于在mongoDB返回匹配的文档之前对其进行操作。 collection.find( { qty: { $gt: 25 } }, { item: 1, qty: 1 }) is a cursor you can do various operations on it. 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 . 所有这些东西都在docs中给出。 I think it's better to use mongoose instead of the native driver because of the features and the popularity. 我认为使用猫鼬代替本地驱动程序会更好,因为其功能和受欢迎程度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM