MongoDB:使用精简聚合函数

编程入门 行业动态 更新时间:2024-10-26 07:30:45
本文介绍了MongoDB:使用精简聚合函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用lean()选项以加快查询速度.但是,当将其添加到这样的查询中时:

I'm trying to use the lean() option in order to speed up my queries. But when adding it to a query like this:

Pets.aggregate({ $match: { 'Name': 'Floofkins', 'lastMealDate': {$gt: today}}}, function (err, pets){ if (err) { res.status(500).json({'error': err}); console.log('Could not fetch pets: ' + err); return; } petsHadMealsToday = pets.length; }).lean();

我得到的只是TypeError: Cannot read property 'lean' of undefined,尽管pets.length返回了与查询匹配的宠物数.

All I get is TypeError: Cannot read property 'lean' of undefined although pets.length returns the number of pets that matched the query.

但是,如果我删除match选项并运行如下所示的内容,它的工作原理就像是一种魅力.

If I'd remove the match option however and run something like below, it works like a charm.

Pets.aggregate({ 'Name': 'Floofkins', 'lastMealDate': {$gt: today}}, function (err, pets){ if (err) { res.status(500).json({'error': err}); console.log('Could not fetch pets: ' + err); return; } petsHadMealsToday = pets.length; }).lean();

我想我缺少有关如何使用match的基本知识,所以请随时教育我!

I guess I'm missing some fundamental point about how to use match and such, so feel free to educate me!

推荐答案

看了一点之后,这就是我对问题的答案:

After reading a bit, this is my take on the answer to my question:

lean(),因为返回的文档是纯JavaScript对象,而不是Mongoose对象.这是因为可以返回任何形状的文档. 来源.

lean() is not needed on an aggregate function as the documents returned are plain JavaScript objects, and not Mongoose objects. This is because any shape of document can be returned. Source.

因此,将lean()添加到aggregate函数将产生错误,因为没有任何内容可以执行lean()函数.

Therefore adding lean() to an aggregate function will generate an error since there is nothing to perform the lean() function upon.

更多推荐

MongoDB:使用精简聚合函数

本文发布于:2023-11-22 09:42:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616929.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!