【问题标题】:how to use aggregate function in meteor如何在流星中使用聚合函数
【发布时间】:2015-02-10 06:15:22
【问题描述】:

我正在处理以下文档

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

我想得到如下输出

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

当我在 mongo shell 中使用以下命令时,它会给出我需要的输出

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

但是当我在我的流星应用程序聚合中使用相同时,它不起作用...... 那么我可以使用 find 或 findOne 方法来做同样的事情吗...... 或者是否有可能在我的流星应用程序中使用聚合函数......

【问题讨论】:

标签: mongodb meteor


【解决方案1】:

通过使用rawCollection,您可以传入您在 mongo shell 中使用的同一管道。

无需安装第三方包即可。

const stats = await MyCollection.rawCollection()
    .aggregate([
      {$match: {_id: 12}}, 
      {$unwind: "$People"}, 
      {$unwind: "$People.person"}, 
      {$match: {"People.uuid": "123", "People.person.uuid" : "add32"}}
    ])
    .toArray();

【讨论】:

    【解决方案2】:

    我使用了 meteorhacks:aggregate 包。它仅适用于服务器端。

    meteor add meteorhacks:aggregate

    MyCollection.aggregate({ 
      $match: { 
        propertyToQuery: 'valueForProperty' 
      }, { 
        $group: {
          _id: '$propertyToGroupBy',
          result: { $operation: '$propertyToPerformOperationOn' }
        }
    });
    

    https://github.com/meteorhacks/meteor-aggregate https://themeteorchef.com/snippets/aggregations-in-mongodb/

    【讨论】:

    • 包似乎没有维护。有几个 fork,但我无法让它们中的任何一个返回值。
    【解决方案3】:

    这是我手动尝试并为我工作的:

    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

    【讨论】:

    • 如果使用 3.6 作为var result = aggregateQuery(pipeline, {cursor: {}});,请确保添加cursor
    【解决方案4】:

    您需要添加一个包来公开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

    【讨论】:

    • 嗨,当我添加包 monbro:mongodb-mapreduce-aggregation 我的应用程序崩溃了.....
    • 错误:一个名为 '/__dummy__/insert' 的方法已经定义在 packages/ddp/livedata_server.js:1444 在 Function._.each._.forEach (packages/underscore/underscore.js :113) 在 _.extend.methods (packages/ddp/livedata_server.js:1442) 在 Mongo.Collection._define MutationMethods (packages/mongo/collection.js:884) 在新的 Mongo.Collection (packages/mongo/collection. js:208) 在包 (packages/monbro:mongodb-mapreduce-aggregatio/server.coffee:7:24) 在 packages/monbro:mongodb-mapreduce-aggregation/server.coffee:1
    • @GopalRao 我在尝试使用这个包的 MapReduce 功能时遇到了同样的错误。
    • 4 年没有更新了。不太可能仍然有效。
    猜你喜欢
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    相关资源
    最近更新 更多