2015-02-10 81 views
6

我正在下面的文件如何使用聚合函数流星

{ 
"_id" : 12, 
"firstName" : "wer", 
"People" : [ 
    { 
     "uuid" : "123", 
     "name" : "sugun", 
     "person" : [ 
      { 
       "uuid" : "add32", 
       "name" : "ssss" 
      }, 
      { 
       "uuid" : "fdg456", 
       "name" : "gfg" 
      } 
     ] 
    }, 
    { 
     "uuid" : "222", 
     "name" : "kiran" 
    } 
] 
} 

我要当蒙戈使用以下命令IAM外壳给它让我的输出如下

{ 
"_id" : 456, 
"People" : [ 
    { 
     "uuid" : "123", 
     "name" : "sugun", 
     "person" : [ 
      { 
       "uuid" : "add32", 
       "name" : "ssss" 
      } 
     ] 
    } 
] 
} 

我需要的输出

db.people.aggregate([ 
    {$match: {_id: 12}}, 
    {$unwind: "$People"}, 
    {$unwind: "$People.person"}, 
    {$match: {"People.uuid": "123", "People.person.uuid" : "add32"}} 
]) 

但是当亚姆在我的流星应用总使用相同的不工作...... 所以我可以使用find或findOne方法来做同样的事情............. 或者如果在我的流星应用程序中使用聚合函数有任何可能性......

+0

在这一点上我当我推荐http://github.com/JcBernack/meteor-reactive-aggregate时,它对我来说工作得很好,并且在过去一周内已经提交了一个提交 – Rohmer 2016-10-10 21:16:31

回答

1

你需要添加一个包,露出aggregate功能:

meteor add monbro:mongodb-mapreduce-aggregation

然后你就可以像使用普通的代码:

var MyCollection = new Mongo.Collection('metrics'); 
var pipeline = [ 
    {$group: {_id: null, resTime: {$sum: "$resTime"}}} 
]; 

var result = MyCollection.aggregate(pipeline); 

只是一对夫妇的事情需要注意,这个工作最好在服务器端。对于需要在客户端使用它的文档,需要包的分支,请检查包的文档:https://atmospherejs.com/monbro/mongodb-mapreduce-aggregation

+0

当我添加软件包monbro时,您好!a:mongodb-mapreduce-聚合我的应用程序崩溃..... – 2015-02-10 09:54:48

+0

@GopalRao你能提供崩溃日志? – Akshat 2015-02-10 09:58:22

+1

错误:在_.extend处的函数._。each ._。forEach(packages/underscore/underscore.js:113)处已经在packages/ddp/livedata_server.js:1444处定义了名为'/ __ dummy __/insert' (package/mongo/collection.js:208)在Mongo.Collection._define MutationMethods(packages/mongo/collection.js:884)处的.methods(packages/ddp/livedata_server.js:1442)包/蒙布罗:mongodb-mapreduce-aggregatio/server.coffee:7:24) at packages/monbro:mongodb-mapreduce-aggregation/server.coffee:1 – 2015-02-10 10:27:08

2

下面是我手工尝试和工作对我来说:

var rawUsers = Meteor.users.rawCollection(); 
var aggregateQuery = Meteor.wrapAsync(rawUsers.aggregate, rawUsers); 
var pipeline = [ 
    {$match: {}}, 
    {$project: {username: 1, profile: 1}} 
]; 
var result = aggregateQuery(pipeline); 

return result; 

了解更多关于Meteor.wrapAsync here