什么mongodb查询将限制嵌套列表的结果

S-K*_*-K' 1 mongodb

我有以下文件:

{
    "username":"test",
    "comments":
    [
        {
            "commentId":"53453445",
            "message":"Hello this is a message"
        },
        {
            "commentId":"53453445",
            "message":"Hello this is a message"
        },
        {
            "commentId":"53453445",
            "message":"Hello this is a message"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

如何限制评论仅显示2个项目?

Ori*_*Dar 5

$slice操作:

db.collection.find( { "username":"test" }, { comments: {$slice: 2 } } );