获取mongodb中两个字段的最大差异

时间:2019-01-23 14:01:26

标签: mongodb aggregation-framework

我是mongoDB的新手。在我想获取两个字段之差的最大值时,这里遇到错误。

这是保存在数据库中的数据的结构:

{
    "_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" : "sth",
            "replies" : [ 
                {
                    "user" : "thedeemon",
                    "upVotes" : 10,
                    "downVotes" : 0,
                    "text" : "sth"
                }, 
                {
                    "user" : "mcandre",
                    "upVotes" : 0,
                    "downVotes" : 5,
                    "text" : "sth"
                }, 
                {
                    "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
}

我想要获得的是upVotesdownVotesrepliescomments的最大值减去值。

db.getCollection('links').aggregate([
    {$project: {
        _id: "$author",
        maxVote: $max: {
                $subtract: ["$comments.upVotes", "$comments.downVotes"]
                }
            } 
     }
])

我不知道如何解决!

1 个答案:

答案 0 :(得分:1)

您可以使用$map来获得每个注释的差异(使用$subtract),然后对映射的$max的输出运行comments。另外,您需要另一个嵌套的$max才能与replies取得区别,请尝试:

db.col.aggregate([
    {
        $project: {
            maxVote: {
                $max: {
                    $map: {
                        input: "$comments",
                        as: "comment",
                        in: {
                            $max: {
                                $concatArrays: [
                                    [ { $subtract: [ "$$comment.upVotes", "$$comment.downVotes" ] } ],
                                    {
                                        $map: {
                                            input: "$$comment.replies",
                                            as: "reply",
                                            in: { $subtract: [ "$$reply.upVotes", "$$reply.downVotes" ] }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
])

打印:

{ "_id" : ObjectId("..."), "maxVote" : 28 }