Средняя агрегация MongoDB не работает должным образом - PullRequest
0 голосов
/ 21 января 2019

Я новичок в базах данных NoSQL.Я хочу показать title, url и avg(ratings).Пример данных выглядит следующим образом:

{
    "_id" : ObjectId("52b3833bd3e98582d2bfb628"),
    "author" : {
        "name" : "Graydon Hoare",
        "email" : "graydon@gmail.com"
    },
    "title" : "Why Rust ditched pure functions",
    "body" : "sth",
    "url" : "http://thread.gmane.org/gmane.comp.lang.rust.devel/3674/focus=3855",
    "date" : ISODate("2013-04-30T13:23:00.000Z"),
    "starred" : 105,
    "ratings" : [ 
        3, 
        5, 
        3, 
        2, 
        4, 
        1, 
        3, 
        3, 
        3, 
        2, 
        3
    ],
    "comments" : [ 
        {
            "user" : "tr0lltherapy",
            "upVotes" : 18,
            "downVotes" : 2,
            "text" : "something",
            "replies" : [ 
                {
                    "user" : "thedeemon",
                    "upVotes" : 10,
                    "downVotes" : 0,
                    "text" : "something"
                }, 
                {
                    "user" : "mcandre",
                    "upVotes" : 0,
                    "downVotes" : 5,
                    "text" : "Performance? There are already a slew of performant languages. Assembler, C, C++, Go. What does Rust actually offer that's new and useful in this category, other than using my favorite abbreviation for the named function keyword, fn?"
                }, 
                {
                    "user" : "lacosaes0",
                    "upVotes" : 30,
                    "downVotes" : 6,
                    "text" : "Particular emphasis on memory safety."
                }
            ]
        }, 
        {
            "user" : "hypster",
            "upVotes" : 30,
            "downVotes" : 2,
            "text" : "tl;dr everybody was type-fu fighting",
            "replies" : [ 
                {
                    "user" : "homoiconic",
                    "upVotes" : 15,
                    "downVotes" : 0,
                    "text" : "Here comes the Big Boss, Hu! Simon Peyton-Jones."
                }
            ]
        }
    ],
    "tags" : [ 
        "Rust", 
        "Computer", 
        "Programming"
    ],
    "draft" : true,
    "published" : true
}

Я пробовал следующий запрос, но он не работает должным образом и в среднем ставит значение null.Я не знаю, как мне это исправить.

db.getCollection('links').aggregate(
    [
        {
            $match: {
                "author.email": /@gmail.com$/
            }
        },
        {
            $project: {
                _id: 0,
                title: 1,
                url: 1,
                avgRatings: {
                    $avg: "$Ratings"
                }
            }
        }
    ])

Ожидаемый результат:

title: "Why Rust ditched pure functions", url: "http://thread.gmane.org/gmane.comp.lang.rust.devel/3674/focus=3855", 
avgRatings: 2.90

1 Ответ

0 голосов
/ 21 января 2019

У вас есть опечатка, $Ratings;используйте $ratings, как показано ниже.Синтаксис агрегации чувствителен к регистру.

db.getCollection('links').aggregate(
    [
        {
            $match: {
                "author.email": /@gmail.com$/
            }
        },
        {
            $project: {
                _id: 0,
                title: 1,
                url: 1,
                avgRatings: {
                    $avg: "$ratings"
                }
            }
        }
    ])
...