环回上的MongoDB聚合

编程入门 行业动态 更新时间:2024-10-24 20:18:01
本文介绍了环回上的MongoDB聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何获取Loopback PersistedModel的总和?

How do I obtain the sum of a Loopback PersistedModel?

关于如何实现该目标,似乎没有文档.

There does not seem to be a documentation on how to achieve that.

如果可能的话,我希望避免查找所有行并将其汇总到Node.js中.

If possible I would like to avoid having to find all the rows and sum it in Node.js.

从 github/strongloop/loopback/issues/890中尝试示例

var bookCollection = Book.getDataSource().connector.collection(Book.modelName);

我遇到了错误

throw new Error('MongoDB connection is not established');

如何获取集合的句柄以在MongoDB集合上手动运行聚合查询?

How do I get a handle on the collection to manually run aggregate query on a MongoDB collection?

推荐答案

最终设法使其正常运行.大多数示例都省略了connect()部分.

Finally managed to get it working. Most examples left out the connect() part.

我的工作代码:

Book.getDataSource().connector.connect(function(err, db) { var collection = db.collection('Book'); var author = Book.getDataSource().ObjectID(authorId); collection.aggregate([ { $match: { authorId: author } }, { $group: { _id: authorId, total: { $sum: "$price" } }} ], function(err, data) { if (err) return callback(err); return callback(null, data); }); });

更多推荐

环回上的MongoDB聚合

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

发布评论

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

>www.elefans.com

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