标签: aggregation-framework

Node.JS + MongoDB聚合框架的平均值

我一直在考虑使用Node和MongoDb作为Android应用程序的评级系统,到目前为止,我已经实现了创建,更新和删除,但是如果我想从平均评级中获取平均评级,该怎么办? D B.这是我给出的数据结构决定:

[
  {
    "_id": "1",
    "ratings": [
      {
        "value": "5",
        "user": "CoolUser1"
      },
      {
        "value": "4",
        "user": "CoolUser2"
      }
    ]
  },
  {
    "_id": "2",
    "ratings": [
      {
        "value": "4",
        "user": "CoolUser3"
      },
      {
        "value": "2",
        "user": "CoolUser4"
      }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

基本上我需要的是给定_id的评级的平均值.

这是我用来了解如何设置mongoDB连接的Update方法:

exports.updateRating = function(req, res) {
    var id = req.params.id;
    var rating = req.body;
    console.log('Updating a rating: ' + id);
    console.log(JSON.stringify(rating));
        ratings.update({'_id': id}, 
                        {$push: rating}, 
                        {upsert:true}, 
                        function(err, result) {
                            if (err) {
                                console.log('Error updating rating: ' …
Run Code Online (Sandbox Code Playgroud)

mongodb node.js aggregation-framework

1
推荐指数
1
解决办法
7208
查看次数

子文档中所有键的总值

我有一个MongoDB集合,其文档看起来像:

{
    '_id': 'doc1',
    'store_A': {'apples': 50, 'oranges':20},
    'store_B': {'oranges': 15}
}
{
    '_id': 'doc2',
    'store_A': {'oranges':10},
    'store_B': {'apples': 15}
}
Run Code Online (Sandbox Code Playgroud)

我如何编写一个聚合命令来为我提供集合中所有文档中每个商店的水果总数而不包括所有允许的水果种类?

结果应如下所示:

{
    '_id': 'Result',
    'store_A_total': {'apples': 50, 'oranges': 30},
    'store_B_total': {'apples': 15, 'oranges': 15}
}
Run Code Online (Sandbox Code Playgroud)

此查询有效,但必须明确指定所有水果类型:

db.collection.aggregate(
{'$group': {'_id': 'Result',
    'store_A_apples': {'$sum': '$Store_A.apples'},
    'store_A_oranges': {'$sum': '$store_A.oranges'},
    'store_B_apples': {'$sum': '$store_B.apples'},
    'store_B_oranges': {'$sum': '$store_B.oranges'}
}},
{'$project': {
    'store_A': {'apples': '$store_A_apples','oranges': '$store_A_oranges'},
    'store_B': {'apples': '$store_B_apples','oranges': '$store_B_oranges'}
}})
Run Code Online (Sandbox Code Playgroud)

是否有更好的方法来构建这些文档以促进此类查询?

mongodb aggregation-framework

1
推荐指数
1
解决办法
592
查看次数

Mongodb Aggregation框架组和排序

我有以下文件结构......

{
  "id":"documentID"
  "sessionId":"sometext"
  "msg":"sometext"
  "time":"date"
}
Run Code Online (Sandbox Code Playgroud)
  • sessionId可以存在于许多文档中

我想聚合文档sessionId,每个会话的结果应该包含与按时间排序的会话相关的消息集.

使用MongoDB聚合框架我该如何实现?

我试图先排序然后分组,但每个会话中的消息由于某种原因没有排序:

{ $sort: { "time": 1 } },
{ "$group" : { 
    "_id" : "$sessionId", 
    "msgs" : { "$addToSet" : "$msg" }
} }
Run Code Online (Sandbox Code Playgroud)

有什么建议?你的回答非常感谢.

sorting mongodb aggregation-framework

1
推荐指数
1
解决办法
5873
查看次数

使用聚合在Mongodb中的复杂条件中进行排序

我创建了一个关于使用已经解决的$ match的主题,但现在我陷入了一个更复杂的例子.例如,考虑我的Json的结构:

{
    user: 'bruno',
    answers: [
        {id: 0, value: 3.5},
        {id: 1, value: "hello"}
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想添加或命令,对'value'中包含的值应用一些转换,只对标识符'id'等于0,例如sort方法.

请考虑以下数据:

db.my_collection.insert({ user: 'bruno', answers: [ {id: 0, value: 3.5}, {id: 1, value: "hello"}]})

db.my_collection.insert({ user: 'bruno2', answers: [ {id: 0, value: 0.5}, {id: 1, value: "world"}]})
Run Code Online (Sandbox Code Playgroud)

我试过用:

db.my_collection.aggregate ({$sort: {"answers": {"$elemMatch": {id: 0, value: -1}}}})
Run Code Online (Sandbox Code Playgroud)

但是,它没有用.预期结果是:{user:'bruno2'answers:[{id:0,value:0.5},{id:1,value:"world"}]},{user:'bruno'answers:[{id: 0,值:3.5},{id:1,value:"hello"}]})

谢谢.

sorting mongodb aggregation-framework

1
推荐指数
1
解决办法
600
查看次数

Mongo的独特值计算Java中的两个字段

我正在使用java连接到MongoDB.我想获取并计算两个字段的不同值,即requestId和telNum.我用谷歌搜索,但没有找到如何获取两个字段的不同值.

java mongodb aggregation-framework

1
推荐指数
1
解决办法
9439
查看次数

与geonear的Mongoose聚合

我正在尝试使用Mongoose geoNear命令实现分页.geoNear不支持跳过,我理解聚合会起作用(带有性能成本的分页).如何将此转换为聚合查询以跳过多个文档?

exports.near = function(req, res) {
    if(req.query.lat && req.query.lng) {
        var point = {
            type: "Point", coordinates : [Number(req.query.lng), Number(req.query.lat)]
        };
        var queryParams = {
            spherical: true,
            skip: 0,
            limit: 10,
            distanceMultiplier: 6371 // radians to kilometers. (The radius of the Earth is approximately 3,959 miles or 6,371 kilometers.)
        };
        if(req.query.q) {
            var matcher = new RegExp(req.query.q, "i");
            queryParams.query = {
                $or: [
                    {'name': matcher },
                    {'address.full': matcher}
                ]
            };
        }
        if(req.query.limit) {
            queryParams.limit = parseInt(req.query.limit, 10);
        }
        if(req.query.offset) …
Run Code Online (Sandbox Code Playgroud)

geospatial mongoose mongodb node.js aggregation-framework

1
推荐指数
1
解决办法
4447
查看次数

如何在MONGODB中进行SQL INTERSECT操作

SELECT SOME_COLUMN 
FROM TABLE 
WHERE SOME_COLUMN_NAME = 'VALUE' 

INTERSECT 

SELECT SOME_COLUMN 
FROM TABLE 
WHERE SOME_COLUMN_NAME_VALUE = 'NEW_VALUE'
Run Code Online (Sandbox Code Playgroud)

如何INTERSECT在MongoDB中获取2个查询(在SQL中使用运算符)的公共值或交集值?

INTERSECT 是SQL的关键字,它是如何为MongoDB完成的?

mongodb mongodb-query aggregation-framework

1
推荐指数
1
解决办法
1394
查看次数

MongoDB:$ match和$ explain

我有以下查询:

uno = db.prueba.aggregate(
    {$project: {a_gt_b: {$cmp: ['$num-a','$num-b']}}},
    {$match: {a_gt_b:{$gt:0}}},
    {$group:{"_id":"$a_gt_b",total:{"$sum":1}}},
    {$project: {"_id":0,"total":1}}
);
Run Code Online (Sandbox Code Playgroud)

我想执行$ explain命令,根据这个答案, 我必须采取$ match部分,但我不知道如何.

mongodb explain nosql aggregation-framework

1
推荐指数
1
解决办法
124
查看次数

mongodb上的多次总数/计数(总和性别和总结果)

我有这个文件:

{gender:"male", ...},
{gender:"male", ...},
{gender:"female", ...},
{gender:"female", ...},
{gender:"female", ...},
Run Code Online (Sandbox Code Playgroud)

所以,我需要像

{
total:5,
male:2,
female:3
}
Run Code Online (Sandbox Code Playgroud)

我的实际查询(没有工作):

db.collection.aggregate([
{
    $match:{...}
},
{
    $group:{
        _id:"$_id",
        gender:{
            $push:"$gender"
        },
        total:{
            $sum:1
        }
    }
},
{
    $unwind:"$gender"
},
{
    $group:{
        _id:"$gender",
        name:{
            $addToSet:"$all"
        },
        "count":{
            $sum:1
        }
    }
}
])
Run Code Online (Sandbox Code Playgroud)

我如何能够追溯性别和总数?谢谢

mongodb aggregation-framework

1
推荐指数
1
解决办法
1094
查看次数

MongoDB:在聚合结果中创建对象

我想在我的聚合结果中将Object作为字段返回,类似于问题中的解决方案。但是,在上述解决方案中,聚合将导致对象数组只有该数组中的一项,而不是独立的对象。例如,类似以下查询的$push操作

 $group:{
     _id: "$publisherId",
     'values' : { $push:{
         newCount: { $sum: "$newField" },
         oldCount: { $sum: "$oldField" } }
     }
}
Run Code Online (Sandbox Code Playgroud)

返回这样的结果

{
        "_id" : 2,
        "values" : [ 
            {
                "newCount" : 100, 
                "oldCount" : 200
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

没有一个像这样

{
        "_id" : 2,
        "values" : {
                "newCount" : 100, 
                "oldCount" : 200
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

后者是我需要的结果。那么,如何重写查询以获得类似的结果?我可以得到最好的结果吗?

mongodb mongodb-query aggregation-framework

1
推荐指数
1
解决办法
2603
查看次数