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

时间:2013-04-25 21:32:22

标签: 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"
        }
    ]
}

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

1 个答案:

答案 0 :(得分:5)

$slice运算符:

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