我有以下文件:
{
"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个项目?
答案 0 :(得分:5)
$slice
运算符:
db.collection.find( { "username":"test" }, { comments: {$slice: 2 } } );