如何获得Loopback PersistedModel的总和?
似乎没有关于如何实现这一目标的documentation。
如果可能的话,我想避免必须找到所有行并在Node.js中加总。
尝试https://github.com/strongloop/loopback/issues/890
中的示例var bookCollection = Book.getDataSource().connector.collection(Book.modelName);
我收到了错误
throw new Error('MongoDB connection is not established');
如何获取集合的句柄以在MongoDB集合上手动运行聚合查询?
答案 0 :(得分:14)
最终成功实现了它。大多数示例都忽略了connect()
部分。
我的工作代码:
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);
});
});
答案 1 :(得分:1)
具有环回的汇总查询
old version below -
import { StackNavigator } from 'react-navigation';
const PrimaryNav = StackNavigator({
Splash: { screen: Splash },
Login: { screen: Login },
}, {
// Default config for all screens
headerMode: 'none',
initialRouteName: 'Splash',
});
New Version below -
import { createAppContainer, createStackNavigator } from 'react-navigation';
const MainNavigator = createStackNavigator({
Splash: { screen: Splash }
},
{
// Default config for all screens
headerMode: 'none',
initialRouteName: 'Splash'
});
const PrimaryNav = createAppContainer(MainNavigator);